排序算法之快速排序代码c++

0
12
 1 #include<iostream>
 2 using namespace std; 
3 Paritition(int A[], int low, int high) { 4 int pivot = A[low]; 5 while (low < high) { 6 while (low < high && A[high] >= pivot) { 7 --high; 8 } 9 A[low] = A[high]; 10 while (low < high && A[low] <= pivot) { 11 ++low; 12 } 13 A[high] = A[low]; 14 } 15 A[low] = pivot; 16 return low; 17 } 18 19 void QuickSort(int A[], int low, int high) //快排母函数 20 { 21 if (low < high) { 22 int pivot = Paritition(A, low, high); 23 QuickSort(A, low, pivot - 1); 24 QuickSort(A, pivot + 1, high); 25 } 26 } 27 28 int main() 29 { 30 int a[10] = {2,5,9,6,4,3,5,10,7,6}; 31 QuickSort(a,0,9); 32 for(int i = 0;i<10;i++) 33 { 34 cout << a[i] <<" "; 35 } 36 }

<

发布回复

请输入评论!
请输入你的名字