public class QuickSort {
/**
*
* @param data
* @param startIndex
* @param endIndex
*/
public static void sort(Comparable[] data, int startIndex, int endIndex) {
if (startIndex < endIndex) {
int middleIndex = partition(data, startIndex, endIndex);
sort(data, startIndex, middleIndex - 1);
sort(data, middleIndex + 1, endIndex);
}
}
public static void sort(Comparable[] data) {
sort(data, 0, data.length - 1);
}
public static void sort(Comparable[] data, int startIndex) {
sort(data, startIndex, data.length - 1);
}
private static int partition(Comparable[] data, int startIndex, int endIndex) {
Comparable pivotElement = data[endIndex];
int i = startIndex - 1;
for (int j = startIndex; j < endIndex; j++) {
if (data[j].compareTo(pivotElement) < 0) {
swap(data, ++i, j);
}
}
swap(data, ++i, endIndex);
return i;
}
private static void swap(Comparable[] data, int i, int j) {
Comparable tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}