Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 24, 2014 18:15
Show Gist options
  • Save gabhi/11264220 to your computer and use it in GitHub Desktop.
Save gabhi/11264220 to your computer and use it in GitHub Desktop.
Quick Sort
public static void quickSort(int[] a, int p, int r)
{
if(p<r)
{
int q=partition(a,p,r);
quickSort(a,p,q);
quickSort(a,q+1,r);
}
}
private static int partition(int[] a, int p, int r) {
int x = a[p];
int i = p-1 ;
int j = r+1 ;
while (true) {
i++;
while ( i< r && a[i] < x)
i++;
j--;
while (j>p && a[j] > x)
j--;
if (i < j)
swap(a, i, j);
else
return j;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment