int partition(int A[], int p, int q)
{
if(A==NULL||p<0||q<p) return 0;
int key = A[q];
int smallindex = p-1;
//swapeint(A[p],A[q]);
for(int i=p; i<q; ++i)
{
if(A[i]<key)
{
++smallindex;
if(smallindex!=i)
{
swap(A[i],A[smallindex]);
}
}
}
smallindex++;
swap(A[smallindex], A[q]);
return smallindex;
}
void swapeint(int& a, int& b)
{
int temp = a;
a=b;
b=temp;
}
void quicksort(int myarray[], int p, int q)
{
int index = partition(myarray,p,q);
if(index>p)
{
quicksort(myarray, p, index-1);
}
if(index<q)
{
quicksort(myarray,index+1,q);
}
}
void test()
{
int myarray[] = {3,4,6,100,88,44,2,0,77,33,1000,50,44,30,10000,99999,22};
quicksort(myarray,0,16);
printf("index: %d \t",111);
}
网友评论