操作系统-多线程排序

Topic:

多线程排序应用程序

 

Requirements:

  1. 编写一个程序将一个整数列表分为两个大小相等的较小的子列表。两个单独线程(我们称为排序进程),采用算法对两个子列表进行排序
  2. 这两个子列表,由第三个线程(称为合并进程)合并成一个已经排好序的线程
  3. 有一个全局的数组,每个排序线程对这个数组的一半进行排序。
  4. 创建另一个与第一个全局数组一样大的全局数组,将排序后的结果放到这个数组中。

 

Procedure

  1. 编写main函数逻辑,需要将两个数组分为两个子数组
  2. 用希尔排序的方法编写sort函数,
  3. 用的递归排序编写merge函数,
  4. 打开虚拟机,将刚刚写好的c代码放入一个文件夹中,在该文件夹下打开终端,输入以下命令

Gcc sorting.c –lpthread

5.命令完成后可以在文件夹中看到a.out文件,在终端输入./a.out运行该文件,得到运行结果

#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>

#define SIZE                10
#define NUMBER_OF_THREADS    3

void* sorter(void* params);    /* thread that performs basic sorting algorithm */
void* merger(void* params);    /* thread that performs merging of results */

int list[SIZE] = { 7,12,19,3,18,4,2,6,15,8 };

int result[SIZE];

typedef struct
{
    int from_index;
    int to_index;
} parameters;

int main(int argc, const char* argv[])
{
    parameters* data_1 = (parameters*)malloc(sizeof(parameters));
    parameters* data_2 = (parameters*)malloc(sizeof(parameters));

    pthread_t tid_sort1;
    pthread_t tid_sort2;
    pthread_t tid_merge;

    data_1->from_index = 0;  /*数组前一半数值*/
    data_1->to_index = 4;
    pthread_create(&tid_sort1, NULL, sorter, (void*)data_1);

    data_2->from_index = 5;  /*数组后一半*/
    data_2->to_index = 9;
    pthread_create(&tid_sort2, NULL, sorter, (void*)data_2);

    pthread_join(tid_sort1, NULL);
    pthread_join(tid_sort2, NULL);

    pthread_create(&tid_merge, NULL, merger, NULL);
    pthread_join(tid_merge, NULL);

    printf("The sorted array is :\n");
    int p = 0;
    for (; p < 10; p++)
        printf("%d ", result[p]);
    return 0;
}


void* sorter(void* params)
{
    parameters* index;
    index = (parameters*)params;
    int temp,len=(index->to_index)-(index->from_index)+1;
    int i, j, k, gap; // gap 为步长
    for (gap = len / 2; gap > 0; gap /= 2) { // 步长初始化为数组长度的一半,每次遍历后步长减半,
        for (i = index->from_index;( i-index->from_index )< gap; ++i) { // 变量 i 为每次分组的第一个元素下标

            for (j = i + gap; j <=index->to_index; j += gap) { //对步长为gap的元素进行直插排序,当gap为1时,就是直插排序
                temp = list[j];  // 备份a[j]的值
                k = j - gap; // j初始化为i的前一个元素(与i相差gap长度)
                while (k >= index->from_index && list[k] > temp) {
                    list[k + gap] = list[k]; // 将在a[i]前且比tmp的值大的元素向后移动一位
                    k -= gap;
                }
                list[k + gap] = temp;
            }
        }
    }
    i=index->from_index;
    printf("The shellsorted array is:");
    for(;i<index->to_index;i++)
      printf("%d ",list[i]);
    printf("\n");   
}

void* merger(void* params) {
    int start = 0, end = 9, mid = 4;
    int i = start;
    int j = mid + 1;
    int temp[end + 1];
    int k = 0;
    while (i <= mid && j <= end) {
        if (list[i] < list[j]) {
            temp[k++] = list[i++];
        }
        else {
            temp[k++] = list[j++];
        }
    }
    if (i == mid + 1) {
        while (j <= end)
            temp[k++] = list[j++];
    }
    if (j == end + 1) {
        while (i <= mid)
            temp[k++] = list[i++];
    }
    for (j = 0, i = start; j < k; i++, j++) {
        result[i] = temp[j];
    }
    pthread_exit(0);
}

猜你喜欢

转载自blog.csdn.net/m0_46785351/article/details/117003300