Title: Assignment II
Reg. No: P58/76125/2012
Name: Koech Charles.
Introduction
Quicksort, also known as partition-exchange sort, uses these steps.
Choose any element of the array to be the pivot.
Divide all other elements (except the pivot) into two partitions.
All elements less than the pivot must be in the first partition.
All elements greater than the pivot must be in the second partition.
Use recursion to sort both partitions.
Join the first sorted partition, the pivot, and the second sorted partition.
The best pivot creates partitions of equal length (or lengths differing by 1). The worst pivot creates an
empty partition (e.g. if the pivot is the first or last element of a sorted array). The runtime of Quicksort
ranges from O(n log n) with the best pivots, to O(n2) with the worst pivots, where n is the number of
elements in the array.
Randomized quicksort
Randomized quicksort has the desirable property that, for any input, it requires only O(n log n) expected
time (averaged over all choices of pivots).
For simplicity, let us assume no two elements in the array are equal — when we are done with the
analysis, it will be easy to look back and see that allowing equal keys could only improve performance.
We now prove the following theorem.
Theorem : The expected number of comparisons made by randomized quicksort on an array of size n is
at most 2n ln n.
Proof: First of all, when we pick the pivot, we perform n - 1 comparisons (comparing all other elements
to it) in order to split the array. Now, depending on the pivot, we might split the array into a LESS of size
0 and a GREATER of size n - 1, or into a LESS of size 1 and a GREATER of size n - 2, and so on, up to a LESS
of size n- 1 and a GREATER of size 0. All of these are equally likely with probability 1=n each. Therefore,
we can write a recurrence for the expected number of comparisons T(n) as follows:
Formally, we are using the expression for Expectation given in (??), where the n different possible splits
are the events
We can rewrite equation (1) by regrouping and getting rid of T(0):
Now, we can solve this by the “guess and prove inductively” method. In order to do this, we first need a
good guess. Intuitively, most pivots should split their array “roughly” in the middle, which suggests a
guess of the form cn ln n for some constant c. Once we’ve made our guess, we will need to evaluate the
resulting summation. One of the easiest ways of doing this is to upper-bound the sum by an integral. In
particular if f(x) is an increasing function, then
which we can see by drawing a graph of f and recalling that an integral represents the “area under the
curve”. In our case, we will be using the fact that
So, let’s now do the analysis. We are guessing that
This guess works for the base case
T(1) = 0 (if there is only one element, then there are no comparisons).
Arguing by induction we have:
In terms of the number of comparisons it makes, Randomized Quicksort is equivalent to randomly
shuffling the input and then handing it off to Basic Quicksort. So, we have also proven that Basic
Quicksort has O(n log n) average-case running time.
Code: Randomized quicksort from introduction in algorithms i.e. C++
#include <cstdlib>
#include <iostream>
using namespace std;
int partition(int a[], int left, int right) {
int i = left;
int j = right;
int temp;
int pivot = a[left];
while(i <= j) {
while(a[i] < pivot)
i++;
while(a[j] > pivot)
j--;
if(i>j) break;
if (i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
} }
return i;
}
int randomized_partition(int a[], int left, int right){
int t = left + rand() % (right - left + 1);
std::swap(a[right], a[t]);
return partition(a, left, right);
}
void randomized_quicksort(int a[], int l, int r){
if(l> = r) return;
int q = randomized_partition(a, l, r);
randomized_quicksort(a, l, q-1);
randomized_quicksort(a, q+1, r);
}
int main(){
int a[] = {12, 6, 4, 7, 9, 10, 11, 14, 13, 19, 18, 21, 20};
int n = sizeof(a) / sizeof(n);
randomized_quicksort(a, 0, n-1);
for (int i = 0; i < n; i++){
cout << a[i] << " " << endl;
}
return 0;
}
Reference:
http://www.cc.gatech.edu
http://wiki.answers.com/Q/Write_A_program_to_implement_randomized_quick_sort_algorithm
© Copyright 2026 Paperzz