预览加载中,请您耐心等待几秒...
1/3
2/3
3/3

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

官方经典快速排序算法/**Thisprogramisfreesoftware;youcanredistributeitand/ormodify*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby*theFreeSoftwareFoundation;eitherversion2oftheLicense,or*(atyouroption)anylaterversion.**Thisprogramisdistributedinthehopethatitwillbeuseful,*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe*GNUGeneralPublicLicenseformoredetails.**YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense*alongwiththisprogram;ifnot,writetotheFreeSoftware*Foundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307USA**Thisprogramdemonstratestheuseofthequicksortalgorithm.For*moreinformationaboutthisandothersortingalgorithms,see*HYPERLINK"http://linux.wku.edu/~lamonml/kb.html"\t"_blank"http://linux.wku.edu/~lamonml/kb.html**/#include<stdlib.h>#include<stdio.h>#defineNUM_ITEMS100voidquickSort(intnumbers[],intarray_size);voidq_sort(intnumbers[],intleft,intright);intnumbers[NUM_ITEMS];intmain(){inti;//seedrandomnumbergeneratorsrand(getpid());//fillarraywithrandomintegersfor(i=0;i<NUM_ITEMS;i++)numbers[i]=rand();//performquicksortonarrayquickSort(numbers,NUM_ITEMS);printf("Donewithsort.\n");for(i=0;i<NUM_ITEMS;i++)printf("%i\n",numbers[i]);}voidquickSort(intnumbers[],intarray_size){q_sort(numbers,0,array_size-1);}voidq_sort(intnumbers[],intleft,intright){intpivot,l_hold,r_hold;l_hold=left;r_hold=right;pivot=numbers[left];while(left<right){while((numbers[right]>=pivot)&&(left<right))right--;if(left!=right){numbers[left]=numbers[right];left++;}while((numbers[left]<=pivot)&&(left<right))left++;if(left!=right){numbers[right]=numbers[left];right--;}}numbers[left]=pivot;pivot=left;left=l_hold;right=r_hold;if(left<pivot)q_sort(numbers,left,pivot-1);if(right>pivot)q_sort(numbers,pivot+1,right);}