操作系统实验三——多线程归并排序
创建一个数组,用sort函数排序并计算所需时间
sort 函数:
void* sort(void *data){
parameters *r=(parameters *)data;
int temp;
int i,j;
for(int i=r->start+1;i<r->end;i++){
for(j=r->start;j<r->end-i+r->start;j++){
if(initlist[j]>initlist[j+1]){
temp=initlist[j];
initlist[j]=initlist[j+1];
initlist[j+1]=temp;
}
}
}
随机初始化待排序的数组
#define NUM 40000
#define MAX 40000
int initlist [NUM];
void initElem()
{
srand((unsigned)time(NULL));
for(int i=0;i<NUM;i++)
initlist[i]=rand()%MAX;
}
其中 NUM表示数组元素个数,MAX表示随机初始化的最大值
整体程序如下:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 40000
#define MAX 40000
int initlist [NUM];
typedef struct
{
int start;
int end;
}parameters;
void* sort(void *data);
void* merge(void *arg);
void initElem()
{
srand((unsigned)time(NULL));
for(int i=0;i<NUM;i++)
initlist[i]=rand()%MAX;
}
int main(int argc,char *argv[])
{
int i;
parameters *a;
a=(parameters *)malloc(sizeof(parameters));
a->start=0;
a->end=NUM;
initElem();
pthread_t sort1;
pthread_create(&sort1,NULL,sort,(void *)a);
pthread_join(sort1,NULL);
/*printf("The sorted array is:\n");
for(i=0;i<NUM;i++){
printf("%d\t",initlist[i]);
}
printf("\n");*/
}
void* sort(void *data){
parameters *r=(parameters *)data;
int temp;
int i,j;
for(int i=r->start+1;i<r->end;i++){
for(j=r->start;j<r->end-i+r->start;j++){
if(initlist[j]>initlist[j+1]){
temp=initlist[j];
initlist[j]=initlist[j+1];
initlist[j+1]=temp;
}
}
}
pthread_exit(0);
}
其中 a->start=0; a->end=NUM;是为了方便后续二分排序,现在就把它们设置为0和NUM就可以了
执行:
二分归并排序
整体原理和上述差不多,只是多了一个merge函数和要创建两个线程
整体代码如下:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM 40000
#define MAX 40000
int initlist [NUM];
int a[NUM/2];
int b[NUM/2];
int sortlist[NUM];
typedef struct
{
int start;
int end;
}parameters;
void* sort(void *data);
void* merge(void *arg);
void initElem()
{
srand((unsigned)time(NULL));
for(int i=0;i<NUM;i++)
initlist[i]=rand()%MAX;
}
int main(int argc,char *argv[])
{
int i;
parameters *a;
parameters *b;
a=(parameters *)malloc(sizeof(parameters));
b=(parameters *)malloc(sizeof(parameters));
initElem();
a->start=0;
a->end=NUM/2;
b->start=NUM/2;
b->end=NUM;
pthread_t sort1;
pthread_t sort2;
pthread_t mer;
pthread_create(&sort1,NULL,sort,(void *)a);
pthread_create(&sort2,NULL,sort,(void *)b);
pthread_join(sort1,NULL);
pthread_join(sort2,NULL);
pthread_create(&mer,NULL,merge,NULL);
pthread_join(mer,NULL);
/*printf("The sorted array is:\n");
for(i=0;i<NUM;i++){
printf("%d\t",sortlist[i]);
}
printf("\n");*/
}
void* merge(void *arg){
int x=NUM/2-1;
int y=NUM-1;
int num=NUM-1;
while(x>=0&&y>=NUM/2){
if(initlist[x]>=initlist[y]){
sortlist[num--]=initlist[x--];
}
else{
sortlist[num--]=initlist[y--];
}
}
while(y>=NUM/2){
sortlist[num--]=initlist[y--];
}
while(x>=0){
sortlist[num--]=initlist[x--];
}
pthread_exit(0);
}
void* sort(void *data){
parameters *r=(parameters *)data;
int temp;
int i,j;
for(int i=r->start+1;i<r->end;i++){
for(j=r->start;j<r->end-i+r->start;j++){
if(initlist[j]>initlist[j+1]){
temp=initlist[j];
initlist[j]=initlist[j+1];
initlist[j+1]=temp;
}
}
}
pthread_exit(0);
}
归并时把二分排序好的结果存储在sortlist中
执行结果: