算法导论 快速排序

#include<iostream>
#include<algorithm>
using namespace std;
int partition(int* &a,int p,int r)
{
int x=a[r];
int i=p-1;
for(int j=p;p<=r-1;p++)
{
if(a[j]<=x)
{
i=i+1;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[r]);
return i+1;
}
void quicksort(int* &a,int p,int r)
{
if(p<r)
{
int q= partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}

int main()
{
const int LEN = 20;
int b[LEN] = {12, 43, 0, -4, 98, 75, 64, 88, 5, 32, 11, 12, 13, 84, 34, 27, -5, -244, 49, 345};
int* a = new int[LEN];
for(int i = 0; i < LEN; i++)
a[i] = b[i];
quicksort(a, 0, LEN - 1);
for(int i = 0; i < LEN; i++)
cout<<a[i]<<endl;
return 0;
}
原文地址:https://www.cnblogs.com/riverphoenix/p/2406623.html