Divide and Conquer
Part02
Lecture06
By Mohammed Alqmase
Quick Sort Algorithm
Quick Sort Algorithm
➢ How does it work
➢ Review Its Design Technique
➢ Analyze its Time and Space Complexity.
▪ Extract Recurrence Relation
▪ Analyze it using Recursion Tree Method
▪ Analyze it using Substitution Method
▪ Analyze it using Master Theorem
Quick Sort Algorithm
➢ Quick Sort is a Divide and Conquer algorithm.
➢ Developed by Tony Hoare in 1960.
➢ Efficient sorting algorithm commonly used in
practical applications.
➢ Works by partitioning an array into sub-arrays and
sorting them recursively.
Quick Sort Terminologies
➢ Pivot: The element selected for partitioning the array.
➢ Partitioning: Rearranging elements such that those smaller than
the pivot are on the left and those greater are on the right.
➢ Sub-arrays: The divided parts of the array after partitioning.
Divide and Conquer Design Technique
➢ Divide: Partition the array into two sub-arrays based on the pivot.
➢ Conquer: Recursively sort the sub-arrays.
How Does Quick Sort Work?
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟑 𝟏 𝟐 𝟒 𝟖 𝟔 𝟓 𝟕 O(n)
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟑 𝟏 𝟐 𝟖 𝟔 𝟓 𝟕
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟑 𝟏 𝟐 𝟖 𝟔 𝟓 𝟕
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟏 𝟐 𝟑 𝟖 𝟔 𝟓 𝟕
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟏 𝟐 𝟑 𝟔 𝟓 𝟕 𝟖
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟐 𝟕
𝟏 𝟑 𝟔 𝟓 𝟖
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟐 𝟕
𝟏 𝟑 𝟔 𝟓 𝟖
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟐 𝟕
𝟏 𝟑 𝟓 𝟔 𝟖
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟐 𝟕
𝟏 𝟑 𝟓 𝟖
𝟔
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟒
𝟐 𝟕
𝟏 𝟑 𝟓 𝟖
𝟔
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
low high
𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟔 𝟖 𝟓 𝟕 𝟏 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟖 𝟓 𝟕 𝟔 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟖 𝟓 𝟕 𝟔 𝟑 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟓 𝟕 𝟔 𝟖 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟓 𝟕 𝟔 𝟖 𝟐 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑗
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟐 𝟕 𝟔 𝟖 𝟓 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟐 𝟕 𝟔 𝟖 𝟓 𝟒 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟐 𝟒 𝟔 𝟖 𝟓 𝟕 𝑃𝑖𝑣𝑜𝑡
𝑖
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high-1:
if arr[j] <= pivot:
i += 1
swap(arr[i], arr[j])
swap(arr[i+1], arr[high])
return i + 1
𝑎𝑟𝑟[ 𝑗 ] ≤ 𝑃𝑖𝑣𝑜𝑡 𝟏 𝟑 𝟐 𝟒 𝟔 𝟖 𝟓 𝟕 𝑃𝑖𝑣𝑜𝑡
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
𝟏 𝟑 𝟐 𝟒 𝟔 𝟖 𝟓 𝟕
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
𝟒 𝟔 𝟖 𝟓 𝟕
𝟏 𝟑 𝟐
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
𝟒 𝟔 𝟖 𝟓 𝟕
𝟏 𝟑 𝟐
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
𝟒
𝟏 𝟑 𝟐 𝟔 𝟖 𝟓 𝟕
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
𝟒
𝟏 𝟑 𝟐 𝟔 𝟖 𝟓 𝟕
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1) T(k)
QuickSort(arr, pivotIndex + 1, high) T(n-k-1)
𝟒
𝟏 𝟑 𝟐 𝟔 𝟖 𝟓 𝟕
T(k) T(n-k-1)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟒
𝟏 𝟐 𝟑 𝟓 𝟔 𝟕 𝟖 O(n)
T(k) T(n-k-1)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟒
𝟐 𝟕 O(n)
𝟏 𝟑 𝟓 𝟔 𝟖
T(k) T(n-k-1)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟒
𝟐 𝟕 O(n)
𝟏 𝟑 𝟓 𝟔 𝟖
T(k) T(n-k-1)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟒
𝟐 𝟕 O(n)
𝟏 𝟑 𝟔 𝟖
𝟓
Best / average case
𝑂 1
T(k) T(n-k-1) 𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖
Best / average case
𝑂 1
T(k) T(n-k-1) 𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛)
𝑛 𝑛
𝑇( ) 𝑇( )
2 2
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖
T(k) T(n-k-1)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖 𝑃𝑖𝑣𝑜𝑡
𝑖
T(k) T(n-k-1)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝟖 𝑃𝑖𝑣𝑜𝑡 O(n)
𝑖
T(k) T(n-k-1)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗 𝟖 𝑃𝑖𝑣𝑜𝑡 O(n)
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕
𝑖
T(k) T(n-k-1)
𝑇(𝑛 − 1) 𝑇(0)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗 𝟖 O(n)
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝑃𝑖𝑣𝑜𝑡
𝑖
T(k) T(n-k-1)
𝑇(𝑛 − 1) 𝑇(0)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗 𝟖 O(n)
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝑃𝑖𝑣𝑜𝑡
𝑖
T(k) T(n-k-1)
𝑇(𝑛 − 1) 𝑇(0)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑗 𝟖
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔 𝟕 𝑃𝑖𝑣𝑜𝑡 O(n)
𝑖
T(k) T(n-k-1)
𝑇(𝑛 − 1) 𝑇(0)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝟖
𝑗 𝟕 𝑃𝑖𝑣𝑜𝑡 O(n)
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔
𝑖
T(k) T(n-k-1)
𝑇(𝑛 − 1) 𝑇(0)
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝟖
𝑗 𝟕 𝑃𝑖𝑣𝑜𝑡 O(n)
𝟏 𝟐 𝟑 𝟒 𝟓 𝟔
𝑖
T(k) T(n-k-1)
Worst case 𝑇(𝑛 − 1) 𝑇(0)
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
𝑇 𝑛 − 1 + 𝑂(𝑛) 𝑛>0
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
Master Theorem
Worst case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
𝑇 𝑛 − 1 + 𝑂(𝑛) 𝑛>0
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
Recursive Tree
Worst case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
𝑇 𝑛 − 1 + 𝑂(𝑛) 𝑛>0
Best / average case
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
Best / average case Recursive Tree
𝑂 1 𝑛≤0 𝑇 𝑛
𝑇 𝑛 =ቐ 𝑛𝑘
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑛 𝑛 𝑛 𝑛
𝑇 𝑇
2 2
𝑛 𝑛 𝑛 𝑛
𝑛 𝑛 𝑇 2 𝑇 2 𝑛
𝑇 2 𝑇 2 2 2 2 2
2 2 𝐾
𝑛 𝑛 𝑛
.
.. 𝑇 3 𝑇 3 𝑛
. 2 2 22 . .
.. .. ..
𝑛 𝑛
𝑇 𝑘 𝑛
2 2𝑘−1
Best / average case Recursive Tree
𝑂 1 𝑛≤0 𝑇 𝑛
𝑇 𝑛 =ቐ 𝑛𝑘
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑛 𝑛 𝑛 𝑛
𝑇 𝑇
2 2
.
.. 𝑛
2𝑘 = 𝑛 𝑛 𝑛 𝑛
𝑇 2 𝑇 2 2
𝑙𝑜𝑔2𝑘 = 𝑙𝑜𝑔𝑛 2 2
𝐾
𝑛 𝑛 𝑛
𝑘 𝑙𝑜𝑔2 = 𝑙𝑜𝑔𝑛 𝑇 3 𝑇 3 𝑛
. 2 2 22 . .
𝑘 = 𝑙𝑜𝑔𝑛 .. .. ..
𝑛 𝑛
𝑂(𝑛𝑙𝑜𝑔𝑛) 𝑇 𝑘 𝑛
2 2𝑘−1
Best / average case
𝑂 1 𝑛≤0 Substitution method
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
2𝑘 = 𝑛
𝑛
𝑇 𝑛 = 2𝑇 +𝑛 𝑙𝑜𝑔2𝑘 = 𝑙𝑜𝑔𝑛
2
𝑛 𝑘 𝑙𝑜𝑔2 = 𝑙𝑜𝑔𝑛
𝑇 𝑛 = 22 𝑇 2 + 2𝑛
2 𝑘 = 𝑙𝑜𝑔𝑛
.
.. 𝑇 𝑛 = 𝑛 𝑇 1 + 𝑛𝑙𝑜𝑔𝑛
𝑛 𝑘 𝑇 𝑛 = 𝑛 + 𝑛𝑙𝑜𝑔𝑛
𝑇 𝑛 = 2 𝑇 𝑘 + 𝑘𝑛
2 𝑂(𝑛𝑙𝑜𝑔𝑛)
Binary Search Algorithm
Binary Search Algorithm
➢ How does it work
➢ Review Its Design Technique
➢ Analyze its Time and Space Complexity.
▪ Extract Recurrence Relation
▪ Analyze it using Recursion Tree Method
▪ Analyze it using Substitution Method
▪ Analyze it using Master Theorem
Master Theorem
𝑶(𝟏) 𝒊𝒇 𝒏 ≤ 𝟎
𝑻 𝒏 =ቐ .
𝒂𝑻 𝒏 − 𝒃 + 𝒇 𝒏 , 𝒏>𝟎
Where : 𝒂 > 𝟎, 𝒃 > 𝟎, 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒇(𝒏).
Case 1: 𝒂 < 𝟏 𝑻(𝒏) = 𝑶( 𝒇(𝒏) )
Case 2: 𝒂 = 𝟏 𝑻(𝒏) = 𝑶( 𝒏 𝒇 𝒏 )
𝒏
Case 3: 𝒂 > 𝟏 𝑻(𝒏) = 𝑶( 𝒇 𝒏 𝒂 𝒃 )
Master Theorem -Example 2
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
𝑇 𝑛 − 1 + 𝑂(𝑛) 𝑛>0
𝑎 = 1
𝑏 = 1
𝑓(𝑛) = 𝑂(𝑛)
𝐶𝑎𝑠𝑒 = 2
𝑇(𝑛) = 𝑂( 𝑛2 )
Master Theorem -Example 7
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
𝑇 𝑛/2 + 𝑂(1) 𝑛>0
Master Theorem –Divide Function
𝑂 1 𝑛≤0
𝑇 𝑛 = 𝑛
𝒂𝑇 + 𝑂(𝑛𝒌 ) 𝑛>0
𝒃
Where : 𝒂 > 𝟎, 𝒃 > 𝟎, 𝒌≥𝟎
Case 1: 𝒌 > log 𝒃 𝒂 𝑻(𝒏) = 𝑶( 𝒏𝒌 )
Case 2: 𝒌 = log 𝒃 𝒂 𝑻(𝒏) = 𝑶( 𝒏𝒌 𝒍𝒐𝒈𝒏)
Case 3: 𝒌 < log 𝒃 𝒂 𝑻(𝒏) = 𝑶( 𝒏log𝒃 𝒂 )
Master Theorem -Example 9
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(1) 𝑛>0
𝑎 = 2
𝑏 = 2
𝑘= 0
𝐶𝑎𝑠𝑒 = 2
𝑇(𝑛) = 𝑂(𝑛 )
Master Theorem -Example 8
𝑂 1 𝑛≤0
𝑇 𝑛 =ቐ
2𝑇 𝑛/2 + 𝑂(𝑛) 𝑛>0
𝑎 = 2
𝑏 = 2
𝑘= 1
𝐶𝑎𝑠𝑒 = 2
𝑇(𝑛) = 𝑂(𝑛 𝑙𝑜𝑔𝑛)
Complexity Analysis of Quick Sort
• Time Complexity:
• Best Case: 𝑶(𝒏 𝒍𝒐𝒈 𝒏)
• Average Case: 𝑶(𝒏 𝒍𝒐𝒈 𝒏)
• Worst Case: 𝑶(𝒏²)
• Worst case occurs when the pivot is the smallest or largest element in the array.
• Space Complexity:
• In-place sorting algorithm: 𝑶(log 𝒏) for recursive stack space.
Summary
• Quick Sort is a Divide and Conquer algorithm.
• Efficient on average but has a worst-case of 𝑶(𝒏²).
• Analyzed using Recursion Tree, Substitution Method, and Master Theorem.
• Important algorithm for practical applications and theoretical studies.