
boolean[], byte[], short[], int[], long[], float[], double[], char[]
Integer[], Double[], String[], Object[]
public class QuickSort {
public static void main(String[] args) {
int[] array = {8, 2, 5, 3, 9, 4, 7, 6, 1};
System.out.println("Before QuickSort : " + Arrays.toString(array));
quickSort(array, 0, array.length-1);
System.out.println("After QuickSort : " + Arrays.toString(array));
}
public static void quickSort(int[] array, int lowIndex, int highIndex){
//0. when only one element left
if(lowIndex >= highIndex) return;
//1. pick a PIVOT.
int pivot = array[highIndex];
//2. PARTITIONING with pointers.
int leftPointer = lowIndex;
int rightPointer = highIndex;
while (leftPointer < rightPointer) {
while (array[leftPointer] <= pivot && leftPointer < rightPointer) {
leftPointer++;
}
while (array[rightPointer] >= pivot && leftPointer < rightPointer) {
rightPointer--;
}
swap(array, leftPointer, rightPointer);
}
//if lp and rp are pointing to the same number, than swap it with the pivot.
swap(array, leftPointer, highIndex);
//3. recursively quick sort
quickSort(array, lowIndex, leftPointer-1);
quickSort(array, leftPointer+1, highIndex);
}
private static void swap (int[] array, int index1, int index2){
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}