快速排序(quickSort)(C语言)
1 // Note:Your choice is C++ IDE 2 #include <stdio.h> 3 void swap(int a[],int i,int j) 4 { 5 int temp=a[i]; 6 a[i]=a[j]; 7 a[j]=temp; 8 } 9 void subSort(int a[],int start,int end) 10 { 11 if(start<end) 12 { 13 int base = a[start]; 14 int low = start+1; 15 int high = end; 16 while(true) 17 { 18 while(low<end && a[low]<=base) 19 ++low; 20 while(high>start && a[high]>=base) 21 --high; 22 if(low<high) 23 { 24 swap(a,low,high); 25 } 26 else 27 { 28 break; 29 } 30 } 31 swap(a,start,high); 32 subSort(a,start,high-1); 33 subSort(a,high+1,end); 34 } 35 36 } 37 void quickSort(int a[],int t) 38 { 39 40 subSort(a,0,t) ; 41 } 42 void disPlay(int a[],int t) 43 { 44 int i=0; 45 for(i=0;i<t;i++) 46 { 47 printf("%d ",a[i]); 48 } 49 } 50 void main() 51 { 52 int a[]={100,-98,2,85,-65,45,26,0,75,45,65,-88,-200}; 53 int t=sizeof(a)/4; 54 printf("\n排序前:\n"); 55 disPlay(a,t); 56 quickSort(a,t); 57 printf("\n排序后:\n"); 58 disPlay(a,t); 59 }