CSE2201: Algorithms
Divide and Conquer Approach
and
Quick Sort
Md. Aktaruzzaman Pramanik
Lecturer
Department of CSE, ULAB 1
Today’s Contents
• Quick Sort
• Complexity Analysis
• Questions for You!
2
Divide & Conquer Approach
The divide-and-conquer paradigm involves three steps at
each level of the recursion:
1. Divide the problem into a number of subproblems.
2. Conquer the subproblems by solving them
recursively. If the subproblem sizes are small
enough, however, just solve the subproblems in a
straightforward manner.
3. Combine the solutions to the subproblems into the
solution for the original problem.
3
Divide & Conquer Approach
4
Quick Sort
• The Quick sort algorithm closely follows the divide-and-
conquer paradigm.
1. Divide:
▪ Partition the array A into 2 subarrays A[p..q-1] and A[q+1..r],
such that each element of A[p..q-1] is smaller than or equal to
each element in A[q+1..r]
2. Conquer:
▪ Recursively sort A[p..q] and A[q+1..r] using Quicksort
3. Combine:
▪ Trivial: the arrays are sorted in place
▪ No additional work is required to combine them
▪ The entire array is now sorted 5
Quick Sort
Divide
•Partition the array A into 2 subarrays A[p..q-1] and A[q+1..r],
such that each element of A[p..q-1] is smaller than or equal
to each element in A[q+1..r]
A[p…q] ≤ A[q+1…r]
6
Quick Sort
Need to find index q (pivot) to partition the array
7
Select a Pivot to compare
j
i
i ← left
j ← right
8
i will Search value > pivot
j will Search value <= pivot
i i j
9
i will Search value > pivot
j will Search value <= pivot
i i j
i i i j j
i j
10
i will Search value > pivot
j will Search value <= pivot
i i j j
i i j j
i j
11
i will Search value > pivot
j will Search value <= pivot
j i ji
12
Algorithm of Quick Sort
QUICKSORT (A, left, right) PARTITION (A, left, right)
1. if (left < right) 1. pivot ← A[left]
2. q = PARTITION(A, left, right)
2. i ← left
3. QUICKSORT (A, left, q-1)
3. j ← right
4. QUICKSORT (A, q+1, right)
4. while (i < j)
5. while ( A[i] < = pivot && i <= right)
6. i = i+1
7. while ( A[j] > pivot )
8. j=j–1
9. if ( i < j )
left right 10. A[i] ↔ A[j]
A 5 3 2 6 4 1 3 7 11. A[j] ↔ pivot
: 12. return j
i j
13
Time Complexity
• Best Case Partitioning
– Partitioning (q) produces two regions of size n/2
• Worst Case Partitioning
– One region has one element and the other has n – 2 elements
Time Complexity
• Best-case partitioning
– Partitioning (q) produces two regions of size n/2
QUICKSORT (A, left, right)
if (left < right)
q = PARTITION(A, left, right)
O(n)
QUICKSORT (A, left, q-1) T(n/2)
QUICKSORT (A, q+1, right) T(n/2)
Complexity:
T(n) = O(n) * [T(n/2) + T(n/2)]
= O(n) * 2T(n/2)
= O(n) * O(lgn)
= O(nlgn) 15
Time Complexity
• Worst-case partitioning
– One region has 1 element and the other has n – 1 elements
QUICKSORT (A, left, right)
if (left < right)
q = PARTITION(A, left, right)
O(n)
QUICKSORT (A, left, q-1) T(1)
QUICKSORT (A, q+1, right) T(n-2)
Complexity:
T(n) = O(n) * [T(1) + T(n-2)]
= O(n) * [1+ O(n)]
= O(n) * O(n)
= O(n2) 16
Complexity Analysis of Three Algorithms
Algorithm Best case Worst case
Selection Sort O(n2) O(n2)
Insertion Sort O(n) O(n2)
Merge Sort O(nlogn) O(nlogn)
Quick Sort O(nlogn) O(n2)
17
Advantage and Drawbacks
▪ Advantages
• The quick sort is regarded as the best sorting algorithm.
• This is because of its significant advantage in terms of efficiency
because it is able to deal well with a huge list of items.
• It sorts in place, no additional storage is required as well.
▪ Disadvantage
• The slight disadvantage of quick sort is that its worst-case
performance is similar to average performances of the bubble,
insertion or selections sorts.
In general, the quick sort produces the most effective and widely used
method of sorting a list of any item size.
18
Question for you
1. Which Algorithm will you choose?
- For Sorted Data List
- For Unsorted Data List
2. Is Quick sort a In-Place Algorithm?
3. Sort the given data set using both the Quick Sort. Show
each steps.
15 8 13 18 15 14
19
Some References
https://visualgo.net/en/sorting
20
Thank you!
21