Friday, 12 September 2014

QUICK SORT

qsort(int A[],int lb,int ub)
{
if(lb>=ub){return;}
int j=position(A,lb,ub);
qsort(A,lb,j-1);
qsort(A,j+1,ub);
}
position(int A[],int lb,int ub)
{
int pivot=A[lb];
int i=lb,j=ub;
while(i<j)
{
while(pivot>=A[i])
{
i++;
}
while(pivot<A[j])
{
j--;
}
if(j>i)
{
A[i]=A[i]+A[j];
A[j]=A[i]-A[j];
A[i]=A[i]-A[j];
}
}
A[lb]=A[j];
A[j]=pivot;
return j;
}
main()
{
int i;
int a[]={2,1,0,7,6,9};
qsort(a,0,5);
for(i=0;i<6;i++)
{
printf("%d\n",a[i]);
}
}

No comments:

Post a Comment