欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-18 18:46:57
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 }