排序算法之快速排序代码c++
程序员文章站
2022-09-26 23:50:33
1 #include 2 using namespace std; 3 Paritition(int A[], int low, int high) { 4 int pivot = A[low]; 5 while (low < high) { 6 while (low < hig ......
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 }