Created
December 8, 2011 04:54
-
-
Save spangenberg/1446158 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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