Course Name: CS302-Design An Analysis of Algorithm: Credit Hours: 3
Course Name: CS302-Design An Analysis of Algorithm: Credit Hours: 3
analysis of Algorithm
Credit Hours: 3
i pivot j
The basic algorithm
While i is to the left of j,
we move i right, skipping over elements smaller than the pivot
We move j left, skipping over elements larger than the pivot
When i and j have stopped,
i is pointing at a large element, and
j is pointing at a small element
If i is to the left of j,
those elements are swapped
The effect is to push a large element to the right and a small
element to the left.
In the example above, i would not move and same for the j
QuickSort :Partitioning strategy
Step 1
8 (0) 1 (1) 4 (2) 9 (3) 6 (4) 3 (5) 5 (6) 2 (7) 7 (8) 0 (9)
i j pivot
QuickSort :Partitioning strategy
Step 1
8 (0) 1 (1) 4 (2) 9 (3) 0 (4) 3 (5) 5 (6) 2 (7) 7 (8) 6 (9)
i j pivot
Start by swapping the pivot with the right,
starting j at right -1
A[i] = 8 > pivot
Stop i right over here
QuickSort :Partitioning strategy
Step 1
8 (0) 1 (1) 4 (2) 9 (3) 0 (4) 3 (5) 5 (6) 2 (7) 7 (8) 6 (9)
i j j pivot
A[j] = 7 > pivot
Move Left
A[j] = 2 < pivot
Stop j right over here
2 (0) 1 (1) 4 (2) 9 (3) 0 (4) 3 (5) 5 (6) 8 (7) 7 (8) 6 (9)
i j pivot
A[j] = 7 > pivot
Move Right
A[j] = 2 < pivot
Stop j right over here
2 (0) 1 (1) 4 (2) 9 (3) 0 (4) 3 (5) 5 (6) 8 (7) 7 (8) 6 (9)
i i i i j j pivot
A[i] = 2 < pivot A[j] = 8 > pivot
Move Right Move Left
A[i] = 1 < pivot A[j] = 5 < pivot
Move Right Stop j right over here
A[i] = 4 < pivot
Move Right
A[i] = 9 > pivot
Stop i right over here
QuickSort :Partitioning strategy
Step 2
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 9 (6) 8 (7) 7 (8) 6 (9)
i j pivot
A[i] = 2 < pivot A[j] = 8 > pivot
Move Right Move Left
A[i] = 1 < pivot A[j] = 5 < pivot
Move Right Stop j right over here
A[i] = 4 < pivot
Move Right Swap A[i] and A[j]
A[i] = 9 > pivot
Stop i right over here
QuickSort :Partitioning strategy
Step 3
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 9 (6) 8 (7) 7 (8) 6 (9)
i i i j i pivot
A[i] = 5 < pivot
Move Right
A[i] = 0 < pivot
Move Right
A[i] = 3 < pivot
Move Right
A[i] = 9 > pivot
Stop i right over here
QuickSort :Partitioning strategy
Step 3
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 9 (6) 8 (7) 7 (8) 6 (9)
j j i pivot
A[i] = 5 < pivot A[j] = 9 > pivot
Move Right Move Left
A[i] = 0 < pivot A[j] = 3 < pivot
Move Right Stop j right over here
A[i] = 3 < pivot i and j have crossed
Move Right So no swap for A[i] and A[j]
A[i] = 9 > pivot Instead Swap A[i] and A[pivot]
Stop i right over here
QuickSort :Partitioning strategy
Step 3
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 6 (6) 8 (7) 7 (8) 9 (9)
j i
pivot
A[i] = 5 < pivot A[j] = 9 > pivot
Move Right Move Left
A[i] = 0 < pivot A[j] = 3 < pivot
Move Right Stop j right over here
A[i] = 3 < pivot i and j have crossed
Move Right So no swap for A[i] and A[j]
A[i] = 9 > pivot Instead Swap A[i] and A[pivot]
Stop i right over here
QuickSort : Recursive calls
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 6 (6) 8 (7) 7 (8) 9 (9)
pivot
2 (0) 1 (1) 4 (2) 5 (3) 0 (4) 3 (5) 6 (6) 8 (7) 7 (8) 9 (9)
i
0 i -1 i+1 N -1
QuickSort : Left Recursive call
i j pivot
QuickSort : Left Recursive call
Movements
i j pivot
QuickSort : Left Recursive call
Swap
i j pivot
QuickSort : Left Recursive call
Step 1: Swap
i j pivot
QuickSort : Left Recursive call
Movements
i j pivot
i & j crossed
QuickSort : Left Recursive call
Swap with the pivot
i pivot
QuickSort : Left Recursive call
Swap with the pivot
i
pivot
QuickSort :Recursive Calls
i
pivot
pivot
2 (0) 1 (1) 0 (2) 3 (3) 4(4) 5 (5)
i
0 i -1 i+1 Right
QuickSort
𝑛
𝑇 𝑛 =2𝑇 2
+ 𝑐𝑛
Complexity?
QuickSort Analysis Worst case
𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑐𝑛
𝑇 𝑛 = [𝑇 𝑛 − 1 ] + 𝑛
𝑇 𝑛 = [𝑇 𝑛 − 2 + (𝑛 − 1)] + 𝑛
𝑇 𝑛 = 𝑇 𝑛 − 2 + [(𝑛 − 1) + 𝑛 ]
𝑇 𝑛 = [𝑇(𝑛 − 3) + (𝑛 − 2)] + [(𝑛 − 1) + 𝑛 ]
𝑇 𝑛 = 𝑇 𝑛 − 3 + [(𝑛 − 2) + (𝑛 − 1) + 𝑛 ]
𝑇 𝑛 = 𝑇(𝑛 − 4) + [ 𝑛 − 3 + (𝑛 − 2) + (𝑛 − 1) + 𝑛 ]
𝑇 𝑛 = 𝑇 𝑛 − 𝑘 + [ 𝒏 − 𝒌 + 𝟏 + 𝒏 − 𝒌 + 𝟐 +..
… + 𝒏 − 𝟑 + (𝒏 − 𝟐) + (𝒏 − 𝟏) + 𝒏 ]
Put k = n
this will make series 1+2+3 … + n