2. This sorting algorithm uses the idea of divide and
conquer.
It finds the element called pivot which divides
the array into two halves in such a way that
elements in the left half are smaller than pivot
and elements in the right half are greater than
pivot.
QUICK SORT
3. Three steps
Find pivot that divides the array into two halves.
Quick sort the left half.
Quick sort the right half.
QUICK SORT
4. Example
Consider an array having 6 elements
5 2 6 1 3 4
Arrange the elements in ascending order using
quick sort algorithm
5. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
6. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
7. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
8. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
9. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
10. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
11. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
Initially pointing to the
First element
12. 0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
Initially pointing to the
First element
We will quick sort this array
14. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
15. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.
16. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.
All element to the LEFT of pivot be SMALLER than pivot.
17. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
18. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
So we will start
from right
19. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
So we will start
from rightAnd move towards left
20. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
21. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
Is Pivot < Right
( 5 < 4)
22. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
NO
Is Pivot < Right
( 5 < 4)
23. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
24. 0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
25. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
26. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot < Left
Now move pivot to right
27. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot < Left
NO
So we swap pivot to the right
28. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
29. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
So we will start
from left
30. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
So we will start
from left
And move towards right
31. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
( 5 > 4)
32. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
YES
( 5 > 4)
33. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
YES
( 5 > 4)
So we move left one position
towards right
34. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
35. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
YES
36. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
YES
So we move left one position
towards right
37. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
38. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
39. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
40. 0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
41. 0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
42. 0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
And move the pivot to left
60. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
61. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
62. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
63. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
64. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
So pivot has divided the array into
two sub array
65. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
Left sub array Right sub array
So pivot has divided the array into
two sub array
66. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
Left sub array Right sub array
So pivot has divided the array into
two sub array We will now quick sort
the left sub array
67. 0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
68. 0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
Left Right
69. 0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right
70. 0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right
71. 0 1 2 3 4 5
1 2 3 4 5 6
Left Right
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Now move the pivot to right
72. 0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right