2018-11-14 01:15:47 +08:00
|
|
|
package Sorts;
|
2018-04-09 18:25:02 +08:00
|
|
|
|
2019-07-06 20:56:05 +08:00
|
|
|
import static Sorts.SortUtils.*;
|
2018-04-09 18:25:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
|
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
2018-04-09 18:41:39 +08:00
|
|
|
* @see SortAlgorithm
|
2018-04-09 18:25:02 +08:00
|
|
|
*/
|
|
|
|
class QuickSort implements SortAlgorithm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method implements the Generic Quick Sort
|
|
|
|
*
|
|
|
|
* @param array The array to be sorted
|
2019-05-09 19:32:54 +08:00
|
|
|
* Sorts the array in increasing order
|
2018-04-09 18:25:02 +08:00
|
|
|
**/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> T[] sort(T[] array) {
|
|
|
|
doSort(array, 0, array.length - 1);
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sorting process
|
|
|
|
*
|
2019-05-09 19:32:54 +08:00
|
|
|
* @param left The first index of an array
|
2018-04-09 18:25:02 +08:00
|
|
|
* @param right The last index of an array
|
|
|
|
* @param array The array to be sorted
|
|
|
|
**/
|
|
|
|
|
|
|
|
private static <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
|
|
|
|
if (left < right) {
|
2019-07-06 20:54:55 +08:00
|
|
|
int pivot = randomPartition(array, left, right);
|
2018-04-09 18:25:02 +08:00
|
|
|
doSort(array, left, pivot - 1);
|
2019-05-09 19:32:54 +08:00
|
|
|
doSort(array, pivot, right);
|
2018-04-09 18:25:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:54:55 +08:00
|
|
|
/**
|
|
|
|
* Ramdomize the array to avoid the basically ordered sequences
|
|
|
|
*
|
|
|
|
* @param array The array to be sorted
|
|
|
|
* @param left The first index of an array
|
|
|
|
* @param right The last index of an array
|
|
|
|
* @return the partition index of the array
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static <T extends Comparable<T>> int randomPartition(T[] array, int left, int right) {
|
|
|
|
int randomIndex = left + (int)(Math.random()*(right - left + 1));
|
|
|
|
swap(array, randomIndex, right);
|
|
|
|
return partition(array, left, right);
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:25:02 +08:00
|
|
|
/**
|
|
|
|
* This method finds the partition index for an array
|
|
|
|
*
|
|
|
|
* @param array The array to be sorted
|
2019-05-09 19:32:54 +08:00
|
|
|
* @param left The first index of an array
|
2018-04-09 18:25:02 +08:00
|
|
|
* @param right The last index of an array
|
2019-05-09 19:32:54 +08:00
|
|
|
* Finds the partition index of an array
|
2018-04-09 18:25:02 +08:00
|
|
|
**/
|
|
|
|
|
|
|
|
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
|
|
|
|
int mid = (left + right) / 2;
|
|
|
|
T pivot = array[mid];
|
|
|
|
|
2019-05-09 19:32:54 +08:00
|
|
|
while (left <= right) {
|
|
|
|
while (less(array[left], pivot)) {
|
2018-04-09 18:25:02 +08:00
|
|
|
++left;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
while (less(pivot, array[right])) {
|
2018-04-09 18:25:02 +08:00
|
|
|
--right;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
if (left <= right) {
|
2018-04-09 18:25:02 +08:00
|
|
|
swap(array, left, right);
|
|
|
|
++left;
|
|
|
|
--right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Driver Program
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
// For integer input
|
2019-05-09 19:32:54 +08:00
|
|
|
Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12, 2, 5, 7, 8, 9, 2, 44, 111, 5};
|
2018-04-09 18:25:02 +08:00
|
|
|
|
|
|
|
QuickSort quickSort = new QuickSort();
|
2019-07-06 20:54:55 +08:00
|
|
|
quickSort.sort(array);
|
2018-04-09 18:25:02 +08:00
|
|
|
|
|
|
|
//Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
|
|
|
|
print(array);
|
|
|
|
|
2019-05-09 19:32:54 +08:00
|
|
|
String[] stringArray = {"c", "a", "e", "b", "d"};
|
2018-04-09 18:25:02 +08:00
|
|
|
quickSort.sort(stringArray);
|
|
|
|
|
|
|
|
//Output => a b c d e
|
|
|
|
print(stringArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|