The document provides an overview of the quick sort algorithm through a step-by-step example. It explains that quick sort (1) divides an array into two sublists based on a pivot element, (2) recursively sorts the two sublists, and (3) combines the sorted sublists. The example shows how quick sort iterates through an array of numbers, swapping elements less than or greater than the pivot until the pivot is in its final sorted position, dividing the array into sublists. It concludes that quick sort generally has a runtime of O(n log n) but can be O(n^2) in the worst case if the pivot element is extremely unbalanced.
2. Quick Sort
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
51 41 21 61 81 101 46 71 105 31 91 76
qa.zariga.com
1. Divide and Conquer design.
2. It works recursively
3. Choose a Pivot element in an array
(Random)
P
i
v
o
t
4. Place this element in such a way, the
array list will get divided into two sublist
so that pivot element will be greater than
first sublist and less than other sublist
3. Quick Sort Scanning from right to left the first
number visited that has value less than 51, thus
exchange both of them
Iteration=1
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
51 41 21 61 81 101 46 71 105 31 91 76
s
t
a
r
t
Left to right search
the number which
less that 51 swap
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 61 81 101 46 71 105 51 91 76qa.zariga.com
Pivot
Element
4. Quick Sort Scanning from left to right the first
number visited that has value greater than 51, thus
exchange both of them
Iteration=2
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 61 81 101 46 71 105 51 91 76
s
t
a
r
t
Right to left search
51<A[n]
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 51 81 101 46 71 105 61 91 76
qa.zariga.com
5. Quick Sort Scanning from right to left the first
number visited that has value less than 51, thus
exchange both of them
Iteration=3
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 51 81 101 46 71 105 61 91 76
s
t
a
r
t
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 81 101 51 71 105 61 91 76
qa.zariga.com
6. Quick Sort Scanning from left to right the first
number visited that has value greater than 51, thus
exchange both of them
Iteration=4
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 81 101 51 71 105 61 91 76
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 51 101 81 71 105 61 91 76
qa.zariga.com
7. Quick Sort Scanning from right to left the first
number visited that has value less than 51, thus
exchange both of them
Iteration=6A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 51 101 81 71 105 61 91 76
s
t
a
r
t
51 >A[n]
No Element Found
Scanning from left to right the first number visited that has value greater than 51,
thus exchange both of them
51<A[n]
No Element Found
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 51 101 81 71 105 61 91 76
qa.zariga.com
8. Quick Sort The Number 51 is Placed
to proper Position
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
31 41 21 46 51 101 81 71 105 61 91 76
Sublist1 has value
less than 51
Sublist1 has value
greater than 51
1. The Whole Process is repeated for
both Sublist1 and Sublis2
2. The Final List what we get as Sorted
List
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[10] A[11] A[12]
21 31 41 46 51 61 71 76 81 91 101 105
qa.zariga.com
9. Quick Sort Analysis
• Worst Case O(n )
– Pivot is the Smallest or Largest element in Array
• Worst Case O(nlogn )
– Each Array is divided into two equla subarray of
size n/2
2
qa.zariga.com