Monday, February 4, 2013

quicksort

source code:


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int Partition(vector<int>& A,int p,int r)
{
int x=A[r];
int i=p-1;
for(int j=p;j<=r-1;j++)
{
if(A[j]<=x)
{
i=i+1;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return i+1;
}

void quicksort(vector<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);
}

}

No comments:

Post a Comment