Lecture-7_Sorting Algorithms_QuickSort
Lecture-7_Sorting Algorithms_QuickSort
1
Today’s Contents
• Quick Sort
• Complexity Analysis
• Questions for You!
2
Divide & Conquer Approach
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
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
13
Time Complexity
Complexity:
Complexity:
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
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