代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct {
int upperbound;
int lowerbound;
}PARM;
#define N 10
int a[N] = {5,1,6,4,7,2,9,8,0,3}; // unsorted data
int print()//print current a[] contents
{
int i;
printf("[ ");
for (i=0 ; i<N; i++)
printf ("%d ", a[i]);
printf("]\n");
}
void *asort(void *aptr)
{
PARM *ap, aleft, aright;
int pivot, pivotIndex,left,right,temp;
int upperbound,lowerbound;
pthread_t me, leftThread, rightThread;
me = pthread_self();
ap =(PARM * ) aptr;
upperbound = ap->upperbound;
lowerbound = ap->lowerbound;
pivot = a [upperbound] ; //pick low pivot value
left = lowerbound - 1; //scan index from left side
right = upperbound; //scan index from right side
if(lowerbound >= upperbound)
pthread_exit (NULL);
while (left < right) { //partition loop
do {left++ ;} while (a[left] < pivot) ;
do { right--;} while (a[right] > pivot) ;
if (left < right ) {
temp = a[ left ] ;
a [left] = a [right] ;
a [right] = temp;
}
}
print ();
pivotIndex = left; // put pivot back
temp = a[pivotIndex];
a[pivotIndex] = pivot;
a[upperbound] = temp;
//start the "recursive threads"
aleft.upperbound = pivotIndex - 1;
aleft. lowerbound = lowerbound;
aright.upperbound = upperbound;
aright. lowerbound = pivotIndex + 1;
printf ("%lu: create left and right threads\n" , me) ;
pthread_create (&leftThread,NULL,asort,(void * ) &aleft) ;
pthread_create (&rightThread,NULL,asort,(void * ) &aright) ;
//wait for left and right threads to finish
pthread_join (leftThread,NULL) ;
printf( "%lu: joined with left & right threads\n" , me) ;
}
int main(int argc,char *argv[])
{
PARM arg;
int i, *array;
pthread_t me, thread;
me = pthread_self ( );
printf ( "main %lu: unsorted array = ", me);
print() ;
arg.upperbound = N-1;
arg. lowerbound = 0 ;
printf ( "main %lu create a thread to do Qs\n" , me) ;
pthread_create(&thread,NULL,asort,(void *)&arg);
// wait for Qs thread to finish
pthread_join (thread,NULL);
printf ( "main %lu sorted array = " , me) ;
print () ;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。