Quick Sort in java








import java.util.*;

class qsort
{

 public static void main(String args[])
 {
Scanner s=new Scanner(System.in);
int n=s.nextInt();

int a[]=new int [n];

for(int i=0;i<n;i++)
a[i]=s.nextInt();

quick_sort(a,0,n-1);

for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
 }


  static void quick_sort(int a[],int st,int en)
  {
 if(st<en)
 {
       int k=pivot_ele(a,st,en);
  quick_sort(a,st,k);
  quick_sort(a,k+1,en);
 }
  }


  static int pivot_ele(int a[],int st,int en)
  {
 int i,j,pivot;
pivot=st;
i=st+1;
j=en;

while(i<=j)
{
while(a[i]<a[pivot] && i<en) i++;

while(a[j]>a[pivot]) j--;

if(i<=j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
i++;
j--;
}
}  ///while close

 int t=a[j];
a[j]=a[st];
a[st]=t;

return j;
    }
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.