Skip to content

Instantly share code, notes, and snippets.

@spangenberg
Created December 8, 2011 04:54
Show Gist options
  • Save spangenberg/1446158 to your computer and use it in GitHub Desktop.
Save spangenberg/1446158 to your computer and use it in GitHub Desktop.
private int[] a;
private int n;
public void sort(int[] a)
{
this.a = a;
n = a.length;
quicksort(0, n - 1);
}
private void quicksort (int lo, int hi)
{
int i = lo,
j = hi;
int x = a[(lo + hi) / 2];
while (i <= j) {
while (a[i] < x) i++;
while (a[j] > x) j--;
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (lo < j) quicksort(lo, j);
if (i < hi) quicksort(i, hi);
}
private void exchange(int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment