public class QuickSort {
public static void main(String[] args) {
int[] arr = {5, 1, 7, 3, 1, 6, 9, 4, 2};
quickSort(arr, 0, arr.length - 1);
for (int i : arr) {
System.out.print(i + "\t");
}
}
private static void quickSort(int[] arr, int leftIndex, int rightIndex) {
if (leftIndex > rightIndex) {
return;
}
int l = leftIndex;
int r = rightIndex;
int key = arr[l];
while (l < r) {
while (l < r && arr[r] >= key) {
r--;
}
arr[l] = arr[r];
while (l < r && arr[l] <= key) {
l++;
}
arr[r] = arr[l];
}
arr[l] = key;
quickSort(arr, leftIndex, l - 1);
quickSort(arr, r + 1, rightIndex);
}
}
Q.E.D.