#include<iostream>
#include<algorithm>
using namespace std;
int PARTITION( int A[], int p, int n)
{
int key = A[n-1];
int i = p - 1;
for(int j = p; j < n-1; j++){
if( A[j] <= key){
i = i + 1;
swap( A[i], A[j]);
}
}
swap( A[i+1], A[n-1]);
return i+1;
}
int QUICK_sort(int A[], int p, int n)
{
int q;
if( p < (n-1)){
q = PARTITION( A, p, n);
QUICK_sort( A, p, q);
QUICK_sort( A, q+1, n);
}
}
int main()
{
int n;
int p = 0;
cin >> n;
int* A=new int[n];
if( n == NULL )
return 0;
for(int i = 0; i < n; i++)
cin >> A[i];
QUICK_sort( A, p, n);
for(int j = 0; j < n; j++){
cout << A[j];
cout << " ";
}
delete []A;
A = NULL;
}
快速排序
最新推荐文章于 2021-11-19 16:57:43 发布