Date: 6/6/2012
COURSE: ICS 801
REG. NO: P58/76170/2012
NAME: Wycliffe Mutangili M.
Description
The easiest way to choose a pivot is picking a random element.
Proof that this works; submit a 2 page write-up and code
-------------------------------------------------------------------------------------------------------------One way to improve the performance of an algorithm like quick-sort is by randomly selecting a pivot,
instead of just making the pivot the head of the list.
Randomized quick-sort algorithm employs random selection of pivots which has advantages over unrandomized selection method. Methods like the use of mean, mode, and median in selection of a pivot
may increase the running time hence affecting the effectiveness of a program. Randomized algorithm
will not spend time performing mathematical functions e.g. calculation of mean to select a pivot; it just
picks an element within the Array.
For example when one is using randomized quick-sort as one of the best known sorting algorithms, the
pivot is selected randomly and on average the constant factor is quite small. For the median case, for
every input Array {A} of length {n}, the average running time of quick-sort with random pivot is
(n log n).
This holds for every input and does not have assumptions on the data. Choosing a good pivot
squarely determines the performance of the algorithm hence influencing the running time of
that given algorithm.
In my example I will basically dwell on randomized quick-sort as the best sorting algorithm to
demonstrate and proof correctness. Let us analyze the pseudo code as follows:
1. Randomized partitioning.
Partition (A, p, r) {input A (A…r)}
i ← RANDOM (p, r)
Exchange A[p] ↔ A[i]
return PARTITION (A, p, r)
Ø Now this is the procedure the randomized quick-sort will call in place of “Partition”.
RANDOMIZED QUICKSORT (A, p, r)
If p<r
Then q ←RANDOMIZED-PARTITION (A, p, r)
RANDOMIZED-QUICKSORT (A, p, q-1)
RANDOMIZED-QUICKSORT (A, q+1, r)
·
Since in the real world the context is prone to randomness in terms of the inputs, we
could randomize the input to get a good average scenario over all inputs by explicitly
permuting the input.
However, we should use random sampling to choose the pivot from the range of the
sub-array A {p.. r}
Therefore by randomizing the partitioning and the recursion enables us to reduce the
worst case performance of 0(n2)
·
·
Code by example:
int random_partition(int* arr, int start, int end)
{
srand(time(NULL));
int pivotIdx = start + rand() % (end-start+1);
int pivot = arr[pivotIdx];
swap(arr[pivotIdx], arr[end]); // move pivot element to the end
pivotIdx = end;
int i = start -1;
for(int j=start; j<=end-1; j++)
{
if(arr[j] <= pivot)
{
i = i+1;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[pivotIdx]);
return i+1;
}
void random_quick_sort(int* arr, int start, int end)
{
if(start < end) {
int mid = random_partition(arr, start, end);
random_quick_sort(arr, start, mid-1);
random_quick_sort(arr, mid+1, end);
}
}
void main()
{
int A[] = {2,5,7,1,10,8,9};
random_quick_sort(A, 0,6);
}
Correctness: proof by induction
Ø The FOR loop maintains the loop invariant: for (int j=start; j<=end-1; j++)
After partitioning the first and the second parts (L, R) the parts will be L<pivot and R>pivot
hence get sorted correctly by recursive calls and the entire array is sorted. This comparison
satisfies the inductive hypothesis.
© Copyright 2026 Paperzz