Skip to content

Instantly share code, notes, and snippets.

@apremalal
Created February 18, 2013 06:18
Show Gist options
  • Save apremalal/4975409 to your computer and use it in GitHub Desktop.
Save apremalal/4975409 to your computer and use it in GitHub Desktop.
Quick sort implementation for integers in java.
import java.util.*;
public class QuickSort
{
int a[];
QuickSort(int a[])
{
this.a=a;
}
int[] sort()
{
quickSort(0,a.length-1);
return a;
}
void quickSort(int p,int r)
{
if(p<r)
{
int q=partition(p,r);
quickSort(p,q-1);
quickSort(q+1,r);
}
}
int partition(int p,int r)
{
int x=a[r];
int i=p-1;
for(int j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
}
}
int temp=a[r];
a[r]=a[i+1];
a[i+1]=temp;
return i+1;
}
}
class Test
{
public static void main(String args[])
{
int a[]={5,4,3,2,1};
QuickSort qs=new QuickSort(a);
System.out.println(Arrays.toString(qs.sort()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment