Monday, February 18, 2019

Quick sort on array of integers

Quick sort is one of the most famous recursive technique available today.
       This short program effectively implement the principle of recursion to sort an array of integer

class p
{
public static int p,l,r;//p means pivot l left limit r denotes right
public static void qsort(int []a,int le,int re)
{
int l1=le;
int r1=re;
p=a[le];
while(le<re)
{
while((a[re]>=p)&&(le<re))
{
re--;
}
if(le!=re)
{
a[le]=a[re];
le++;
}
while((a[le]<=p)&&(le<re))
{
le++;
}
if(le!=re)
{
a[re]=a[le];
r--;
}
}
a[le]=p;
p=le;
le=l1;
re=r1;
if(le<p)
qsort(a,l,p-1);
if(re>p)
qsort(a,p+1,re);
}
public static void main(String args[])
{
int as[]={4,3,5,6,8,1,0};
qsort(as,0,6);
for(int i=0;i<7;i++)
System.out.println(i+" ");
}
}

No comments:

Post a Comment