The idea of classic quick sort is to divided the original array to two parts, and the when you doing the partition, you should make sure the element in left subarray always less than a pivot, and the element in right subarray always greater than a pivot. Then make the new subarrays as input and recursively call the method. The different between the classic quick sort and randomized quick sort is how to choose a suitable pivot. For the classic quick sort you can choose the first one or the last one as a pivot, but for the randomized quick sort, you need to choose the pivot depends a random-number generator.
For the classic quick sort, time is often O(nlgn), but if the input is ordered already, it will be down to the O(n2), so the randomized quick sort was invented, it produce a pivot from a random-number generator, and make sure the running time is independent of the input ordering, it makes no assumptions about the input distribution. The time is always O(nlgn).
The detail steps of classic quick sort algorithm are as follow:
1. partition the array to two parts.
1.1.choose the first element as a pivot. Make sure there are two indicators to do the moving, make left indicator point to the pivot, and right indicator point to the next element behind the pivot.
1.2.check the element that the right indicator is pointing. if it is less than the pivot, move the left indicator to the next one, and swap the elements pointing by two indicators. if it is greater than the pivot, only move the right indicator to the next one, and hold on the left indicator.
1.3.after the checking, the element position that is pointing by the left indicator is where the pivot should be. so swap the first element with the left indicator pointing one and return that position at last.
2. make the two subarrays as new input parameters and call the method respectively.
The detail steps of randomized quick sort algorithm are very similar with the classic quick sort. The difference is only you should use a random-number generator to produce a pivot.
The complexity of randomized quick sort is always O(nlgn).
The following is the implement of classic quick sort by java.
private void quickSort(int[] array, int left, int right){
if(left < right){
int pi = partition4QuickSort(array,left,right);
quickSort(array,left,pi-1);
quickSort(array,pi+1,right);
}
}
private int partition4QuickSort(int[] array, int left, int right){
int pivot = array[left];
int i = left;
for(int j = i + 1;j < right;j++){
if(array[j] < pivot){
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i];
array[i] = array[left];
array[left] = temp;
return i;
}
The following is the implement of the randomized quick sort.
private void randomizedQuickSort(int[] array, int left, int right){
if(left < right){
int pi = partition4RandomizedQuickSort(array,left,right);
randomizedQuickSort(array,left,pi-1);
randomizedQuickSort(array,pi+1,right);
}
}
private int partition4RandomizedQuickSort(int[] array, int left, int right){
Random random = new Random();
int r = random.nextInt(right-left)+left;
int temp = array[left];
array[left] = array[r];
array[r] = temp;
return partition4QuickSort(array,left,right);
}
private int partition4QuickSort(int[] array, int left, int right){
int pivot = array[left];
int i = left;
for(int j = i + 1;j < right;j++){
if(array[j] < pivot){
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i];
array[i] = array[left];
array[left] = temp;
return i;
}