[c++]代码库
#include <iostream>
#include <algorithm>
using namespace std;
const int size = 1000;
int partition ( int a[], int low, int high )
{
//选择第一个a[low]作为划分的临界值
while ( low < high )
{
while ( low < high && a[low] <= a[high] ) high--;
swap ( a[low], a[high] );
while ( low < high && a[low] <= a[high] ) low++;
swap ( a[low], a[high] );
}
return low;
}
void Qsort ( int a[], int low, int high )
{
if ( low > high ) return;
int p = partition ( a, low, high );
Qsort ( a, low, p-1 );
Qsort ( a, p+1, high );
}
int main()
{
int N, arr[size];
int i;
while ( cin >> N )
{
for ( i = 0; i < N; ++i )
cin >> arr[i];
Qsort ( arr, 0, N-1 );
for ( i = 0; i < N-1; ++i )
cout << arr[i] << " ";
cout << arr[i] << endl;
}
return 0;
}