https://jordanspencerwu.github.io/randomized-quick-sort/
https://www.geeksforgeeks.org/quick-sort/
https://www.geeksforgeeks.org/stable-quicksort/
public static void RandomizedQuickSort(int[] array, int startIndex, int endIndex) {
if (startIndex < endIndex) {
int pivot = randomizedPartition(array, startIndex, endIndex);
RandomizedQuickSort(array, startIndex, pivot - 1);
RandomizedQuickSort(array, pivot + 1, endIndex);
}
}
public static int randomizedPartition(int[] array, int startIndex, int endIndex) {
int randomEndIndex = randomNumberBetween(startIndex, endIndex);
swap(array, endIndex, randomEndIndex);
return partition(array, startIndex, endIndex);
}
public static int randomNumberBetween(int startNumber, int endNumber) {
return (int) Math.floor(Math.random() * (endNumber - startNumber + 1) + startNumber);
}
public static int partition(int[] array, int startIndex, int endIndex) {
int pivotValue = array[endIndex];
int pivotIndex = startIndex;
for (int j = startIndex; j < endIndex; j++) {
if (array[j] <= pivotValue) {
swap(array, pivotIndex, j);
pivotIndex = pivotIndex + 1;
}
}
swap(array, pivotIndex, endIndex);
return pivotIndex;
}
https://www.geeksforgeeks.org/quick-sort/
https://www.geeksforgeeks.org/stable-quicksort/
A sorting algorithm is said to be stable if it maintains the relative order of records in the case of equality of keys.
Input : (1, 5), (3, 2) (1, 2) (5, 4) (6, 4)
We need to sort key-value pairs in the increasing order of keys of first digit
There are two possible solution for the two pairs where the key is same i.e. (1, 5) and (1, 2) as shown below:
OUTPUT1: (1, 5), (1, 2), (3, 2), (5, 4), (6, 4)
OUTPUT2: (1, 2), (1, 5), (3, 2), (5, 4), (6, 4)
A stable algorithm produces first output. You can see that (1, 5) comes before (1, 2) in the sorted order, which was the original order i.e. in the given input, (1, 5) comes before (1, 2).Second output can only be produced by an unstable algorithm. You can see that in the second output, the (1, 2) comes before (1, 5) which was not the case in the original input.
Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.
QuickSort is an unstable algorithm because we do swapping of elements according to pivot’s position (without considering their original positions).
QuickSort is an unstable algorithm because we do swapping of elements according to pivot’s position (without considering their original positions).
Quicksort can be stable but it typically isn’t implemented that way. Making it stable either requires order N storage (as in a naive implementation) or a bit of extra logic for an in-place version.
In below implementation, we use extra space. The idea is to make two separate lists:
1) First list contains items smaller than pivot.
2) Second list contains items greater than pivot.
In below implementation, we use extra space. The idea is to make two separate lists:
1) First list contains items smaller than pivot.
2) Second list contains items greater than pivot.