Design & Analysis of Algorithms: DR Anwar Ghani
Design & Analysis of Algorithms: DR Anwar Ghani
Dr Anwar Ghani
- Pivot selection
- Partitioning
- Best case
- Worst case
- Average case
Quick Sort
Phases
The quick sort is a divide-and-conquer procedure. It works in two phases, which are
referred to as partitioning and recursive call
Phase #1: In this phase an array element is chosen as a pivot The array is partitioned into two
subarrays such that all elements in the left partition are less than or equal to the pivot, and all
elements in right partition are greater than or equal to the pivot. An illustration of
partitioning is as follows. The left-most key 51 is selected as the pivot.
51 67 11 78 90 13 66 80 92 30 40 59 68 71 65 23
pivot
23 11 13 30 40 51 67 78 90 66 80 92 59 68 71 65
11 13 23 30 40 51 59 65 66 67 68 71 78 80 90 92
Several strategies are used for the selection of pivot. Some common approaches are:
(a) Left-most key
(b) Right-most key
(c) Middle key
(d) Key in a random location
(e) Median of three randomly selected keys
Some of the choices lead to bad partitioning, in which case the left and the right partitioned
are not evenly balanced. Consequently, the performance of quick sort is degraded.
Quick Sort Partitioning
Schemes
• As discussed, the partitioning is based on the choice of pivot. Once a pivot has been
selected, the array is scanned to partition it into two subarrays. There are several schemes for
scanning. Generally, two pointers are used for this purpose
• In one scheme, both the pointers are moved in the left-to-right or right-to-left direction to
examine the stored keys and compare them with the pivot. The keys are swapped depending
upon the outcome of comparison.
• Another approach moves the pointer, alternately, from right-to-left and left-to-right until a
key smaller than and a key larger than pivot are found. The keys are then swapped. This
method is referred to as Hoare Partitioning
Hoare Partitioning
Algorithm
A common partitioning procedure, due originally to Hoare , is as follows
Step# 1: Select left-most element as pivot Define two variables i, j to scan the array
Use variable i as left pointer, and j as right pointer to scan the array
Step# 2: Scan array from right-to-left, by decreasing j, until a key smaller than
or equal to pivot is encountered
Step# 3: Scan array from left-to-right, by increasing i, until key larger than
or equal to pivot is encountered
i j
indexes 1 2 3 4 5 6 7 8 9 10
keys 34 61 30 65 33 59 18 10 15 45
Sample Array
• The array will be partitioned using Hoare algorithm. Two pointers are used to scan the array,
alternately, from right-to-left and left to-right. The pointers are integer variables i and j, which
initially refer to the indexes of the first and last keys in the array
Hoare Partitioning
Example
The left-most key 34 is chosen as pivot. The variables i, j are used to scan the array . The
partitioning steps are illustrated in the following figures. The keys in the left partition are
shaded blue, and those in the right partition are shaded pink. The swapped keys are shaded
green
(1) The left pointer i is positioned at cell 1 of i j
Pivot=34
the array. The right pointer j is positioned at
cell 10. Scanning is started from right-to-left 1 2 3 4 5 6 7 8 9 10
34 61 30 65 33 59 18 10 15 45
1 2 3 4 5 6 7 8 9 10
15 10 30 18 33 59 65 61 34 45
Consider, for example, the sample unsorted array shown below. The quick sort steps are
shown the next diagram
34 61 30 65 33 59 18 10 15 45
Sample unsorted array
Quick Sort
Example
The following diagram illustrates the quick sort procedure using Hoare’s partitioning method. The sorted
elements are numbered and shown in bloe color
Unsorted Array 34 61 30 65 33 59 18 10 15 45
pivot =34 34 61 30 65 33 59 18 10 15 45
15 10 30 18 33 59 65 61 34 45
1
10 15 30 18 33 45 34 61 65 59
2 6 7 8
15 30 18 33 34 45 59 65 61
3 9 10
18 30 33 61 65
4 5
30 33
Sorted Array 10 15 18 30 33 34 45 59 61 65
Quick Sort
Partitioning
Quick sorting is implemented using two procedures, namely, PARTITION and QUICKSORT
The PARTITION consists of two counters to scan the array from left-to-right and then from
right-to-left. (Source: T.Cormen et al) The first element in the array is chosen as the pivot
PARTITION( A, p, r)
x ← A[p] ► left-most element is chosen as pivot. It is stored in x
i ← p-1 ► Index i scans the array from left-to-right
j ←r+1 ►Index j scans the array from right-to-left
while TRUE do ►A loop is set up to scan, which terminates when pointers cross
repeat j ← j-1 ►right pointer is moved until a key smaller than or equal to the
until A[j] ≤ x pivot is found
repeat i ←i+1 ►Left pointer is moved until a key larger than or equal to the
until A[i] ≥ x pivot is found
Visualization
Visualization Example
Quick Sort
Algorithm
The QUICKSORT procedure is recursive. It is used to sort left and right partitions .
QUICKSORT(A, p ,r)
if p < r then ► Recursion is terminated when boundaries cross-over
Visualization
Visualization Example
Analysis of Quick Sort
Quick Sort Analysis
Best Case Scenario
In best case, the array is partitioned in to two nearly equal subarrays
pivot
Left partition Right partition
The recurrence relation for best running time is
T(n) = 2 T( n / 2) + n-1
= n + n lg n – ( n - 1) (closed form )
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
Quick Sort Partitions
Presorted Input
(4) Since key 60 is larger than pivot 10, the j
i Pivot=10
right pointer is moved to the next left cell
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
Quick Sort Partitions
Presorted Input
i j
(7) Since key 40 is larger than pivot 10, the
right pointer is moved to the next left cell
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
i j
(9) Since key 20 is larger than the pivot 10,
the pointer j is moved to the next left cell
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
i j
(10) Since key 20 is larger than the pivot 10,
the pointer j is moved to the next left cell.
The left and right pointers are now 1 2 3 4 5 6 7 8 9
positioned at the same cell. The array is 10 20 30 40 50 60 70 80 90
partition into a single partition to the right
Quick Sort Partitions
Reverse Sorted Input
If the input array is reverse sorted, the quick sort partitions degenerate into a single left
partition. This scenario is demonstrated by the following example. The input keys are {90, 80,
70, 60, 50, 40, 30, 20, 10}
(1) The left pointer i is positioned at cell 1 of j
i Pivot=90
the array. The right pointer j is positioned at
cell 9. Key 90 in the left-most cell is chosen
as pivot. Scanning is started from right-to- 1 2 3 4 5 6 7 8 9
left 90 80 70 60 50 40 30 20 10
i j
(2) Since key 10 is smaller than the pivot
90, the right-to-left scan is stopped
1 2 3 4 5 6 7 8 9
90 80 70 60 50 40 30 20 10
i j
(9) Since key 30 is smaller than the pivot
90, the left pointer is moved to the next right 1 2 3 4 5 6 7 8 9
cell
10 80 70 60 50 40 30 20 90
1 2 3 4 5 6 7 8 9
(10) The left and right pointers are now
10 80 70 60 50 40 30 20 90
positioned at the same cell. The partitioning
procedure terminates. The array is
partitioned into a single left partition Left partition
Quick Sort Analysis
Worst Case Scenario
Consider the case when left partition is empty
2nd cell 1/n T(1)+T(n-2) A[1] A[2] A[3] - - - - A[n-2] A[n-1] A[n]
1 key (n-2 )keys
3rd cell 1/n T(2)+T(n-3) A[1] A[2] A[3] - - - - A[n-2] A[n-1] A[n]
2 keys (n-3) keys
kth cell 1/n T(k-1)+T(n-k) A[1] A[2] A[3] A[4] A[k-1] A[k] - A[n-2] A[n-1] A[n]
k-1 keys (n-k) keys
n-1th cell 1/n T(n-2)+T(1) A[1] A[2] A[3] A[4] - - - A[n-2] A[n-1] A[n]
(n-2 )keys 1 key
nth cell 1/n T(n-1)+T(0) A[1] A[2] A[3] A[4] - - - A[n-2] A[n-1] A[n]
(n-1) keys
Quick Sort Analysis
Average Case Recurrence
A[1] A[2] A[3] - A[k-1] A[k] A[k+1] - A[n-1] A[n]
pivot
Left partition of size k-1 right partition of size n- k
By summing over running times for all costs and associated probabilities
the expected running time, T(n), can be expressed as the recurrence:
∑
1
T(n) = n–1 + — [ T( k - 1) + T(n – k) ]
n
k =1
(n-1).T(n-1) = (n-1)(n – 2) + 2 ∑ T( k - 1)
k =1
……(4)
Quick Sort Analysis Cont’d-1
n −1
(n-1).T(n-1) = (n-1)(n – 2) + 2 ∑ T( k - 1)
k =1
……………(6)
Defining S(n) as
T(n)
S(n) = ———
n+1
∑ ∑
2( i - 1) 2(i+1)-4
S(n) = ——— =
i =1 i (i +1) i =1 i(i +1)
n n
=
i
∑ i =1
2
i(i+1)
- ∑ 4
n
…………..(11)
i =1
Performing summation of harmonic series ,
n
∑ —1i = ln( n )+ 0.386
i =1
…………..(12)
n
∑ i(i+1) = ∑ 1 n
1
Also,
i =1 i =1 i
- ∑
i =1
1
(i+1)
= 1- 1/(n+1)=n/(n+1) …………..(13)
Tworst(n)= n (n -1) / 2
T(n)
Tbest(n) = n lg n + 1