1 #include "stdafx.h" 2 #include "stdlib.h" 3 4 int Partition(int *arr,int _low,int _high); 5 void QSort(int *arr,int low,int high); 6 void QuickSort(int *arr,int low,int high); 7 void Swap_a(int *arr,int i,int j); 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 int arr[10] = {1,2,0,9,3,5,4,6,8,7}; 11 QuickSort(arr,0,9); 12 13 for(int i=0;i<10;++i) 14 { 15 printf("%d\t",arr[i]); 16 } 17 system("pause"); 18 return 0; 19 } 20 21 int Partition(int *arr,int low,int high) 22 { 23 int pivotkey; 24 pivotkey = *(arr+low); 25 while(low<high) 26 { 27 while(low<high && *(arr+high)>=pivotkey) 28 high--; 29 Swap_a(arr,low,high); 30 while(low<high && *(arr+low)<=pivotkey) 31 low++; 32 Swap_a(arr,low,high); 33 34 } 35 return low; 36 } 37 38 void QSort(int *arr,int low,int high) 39 { 40 int pivot; 41 if (low<high) 42 { 43 pivot = Partition(arr,low,high); 44 QSort(arr,low,pivot-1); 45 QSort(arr,pivot+1,high); 46 } 47 } 48 void QuickSort(int *arr,int low,int high) 49 { 50 QSort(arr,low,high); 51 } 52 53 void Swap_a(int *arr,int i,int j) 54 { 55 int tmp = *(arr+i); 56 *(arr+i) = *(arr+j); 57 *(arr+j) = tmp; 58 }
时间: 2024-10-05 09:44:01