I would like to check if this is a correct implementation of QuickSort, It seems to be doing the job, but Am I missing out anything?
public class QuickSort implements Sorter {
public void sort(Comparable[] items) {
QuickSort(items, 0, items.length - 1);
}
static void QuickSort(Comparable[] items, int a, int b) {
int lo = a;
int hi = b;
if (lo >= hi) {
return;
}
Comparable mid = items[(lo + hi) / 2];
Comparable T;
while (lo < hi) {
while (items[lo].compareTo(mid)<0) {
lo++;
}
while (mid.compareTo(items[hi])<0) {
hi--;
}
if (lo < hi) {
T = items[lo];
items[lo] = items[hi];
items[hi] = T;
}
}
QuickSort(items, a, lo);
QuickSort(items, lo == a ? lo + 1 : lo, b);
}
}
fixed:
private static void quickSort(Comparable[] items, int a, int b) {
int i = a;
int j = b;
Comparable x = items[(a+ b) / 2];
Comparable h;
do {
while (items[i].compareTo(x) < 0) {
i++;
}
while (items[j].compareTo(x) > 0) {
j--;
}
if (i <= j) {
h = items[i];
items[i] = items[j];
items[j] = h;
i++;
j--;
}
} while (i <= j);
if (a < j) {
quickSort(items, a, j);
}
if (i < b) {
quickSort(items, i, b);
}
}