1 #include<stdio.h> 2 3 void quick_sort(int array[],int left,int right); 4 void quick_sort(int array[],int left,int right) 5 { 6 int i =left,j = right; 7 int temp; 8 int pivot; 9 10 pivot = array[(left+right) / 2]; 11 12 while (i<=j)//结束判断条件 13 { 14 while (array[i] <pivot)//从右向左找小于基准的元素 15 { 16 i++; 17 } 18 while (array[j] > pivot)//从左向右找小于基准的元素 19 { 20 j--; 21 } 22 if (i <=j)//交换前提 23 { 24 temp = array[i]; 25 array[i] = array[j]; 26 array[j] = temp; 27 i++; 28 j--; 29 30 } 31 } 32 33 if (left < j) 34 { 35 quick_sort(array,left,j);//这里的输入提供了右边新数组 36 } 37 if (i < right) 38 { 39 quick_sort(array,i,right);//提供了左边的新数组 40 } 41 42 43 } 44 45 int main(void) 46 { 47 int array[] = {73,108,111,101,123,2421,23,124,123,1244,23,3}; 48 int i,length; 49 50 length = sizeof(array) / sizeof(array[0]); 51 quick_sort(array,0,length-1); 52 for (i = 0;i<length;i++) 53 { 54 printf("%d ",array[i]); 55 } 56 57 58 }