Recursive Quick Sort in Java

/* Quick Sort in Java  */ 

import java.io.*;
public class quick
{
  void quicksort(int a[],int l,int h)throws IOException
  {
      int x;
      if(l < h)
      {
        x=partition(a,l,h);
        quicksort(a,l,x-1);
        quicksort(a,x+1,h);
      }
  }
  int partition(int a[],int l,int h)throws IOException
  {
     int i=l,j=h,pv=a[l];
     while(i<=j)
     {       
         while(a[i]<=pv)    i++;    
         while(a[j]>pv)     j--;
         if(i<j)
         {
             int temp=a[i];
              a[i]=a[j];
              a[j]=temp;
         }
    }
    int temp1=a[l];
    a[l]=a[j];
    a[j]=temp1;
    return (i-1);
}
public void main(String args[])throws IOException
{
    System.out.print("\nProgram to sort array using quick sort : \n");
    int n,i;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\nEnter array capacity : ");
    n=Integer.parseInt(br.readLine());
    int a[]=new int[n];
    for(i=0;i<n;i++)
        a[i]=(int)(Math.random()*100);          // Generates random numbers 
   System.out.print("\nArray before sorting : ");
    for(i=0;i<n;i++)
        System.out.print(a[i] + "   ");
        quicksort(a,0,n-1);
    System.out.print("\nArray after sorting : ");
    for(i=0;i<n;i++)
        System.out.print(a[i] + "   ");

    }
}

Top