Quicksort: - Sort An Array A (P R) - Divide
Quicksort: - Sort An Array A (P R) - Divide
1
Quicksort A[p…q] ≤ A[q+1…r]
• Conquer
• Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
• Trivial: the arrays are sorted in place
• No additional work is required to combine them
• The entire array is now sorted
2
Apply Quick Sort Algorithm
32 99 29 98 67 45 25 76 33 22 47
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 40 60 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 40 60 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 40 60 99 22 88 66
Interchange 44 and 22 to obtain the list
22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 40 60 99 44 88 66
Interchange 44 and 55 to obtain the list
22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 40 60 99 55 88 66
Interchange 44 and 40 to obtain the list
22 33 11 40 77 90 44 60 99 55 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 40 77 90 44 60 99 55 88 66
Interchange 44 and 77 to obtain the list
22 33 11 40 44 90 77 60 99 55 88 66
44
22 33 11 40 90 77 60 99 55 88 66
22 33 11 40
Apply Quick Sort Algorithm
32 99 29 98 67 45 25 76 33 22 47
32 99 29 98 67 45 25 76 33 22 47
45
32 99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
32 45
99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
99 45
32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45
32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45 47
32 29 98 67 25 76 33 22
Find Smaller Value from Right Side so that Item
< Pivot
99 45
32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45 22
32 29 98 67 25 76 33 47
Exchange
Occurred
45
22 99
32 29 98 67 25 76 33 47
Exchange
Occurred
And
Index Changed
45
32 22 29 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
29 45
32 22 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 22 29 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
98 45
32 22 29 67 25 76 33 99 47
Find Smaller Value from Right Side so that Item
< Pivot
98 45 33
32 22 29 67 25 76 99 47
Exchange
Occurred
98 45 33
32 22 29 67 25 76 99 47
Exchange
Occurred
45
33 98
32 22 29 67 25 76 99 47
Exchange
Occurred
45
32 22 29 33 67 25 76 98 99 47
Exchange
Occurred
And
Index Changed
45
32 22 29 33 67 25 76 98 99 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 22 29 33 67 25 76 98 99 47
Find Greater Value from Left Side so that Item>
Pivot
67 45
32 22 29 33 25 76 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45 76
32 22 29 33 25 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45
32 22 29 33 25 76 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45 25
32 22 29 33 76 98 99 47
Exchange
Occurred
25 45 67
32 22 29 33 76 98 99 47
Exchange
Occurred
45
25 67
32 22 29 33 76 98 99 47
Left Side< 45 < Right
Side
45 67 76 98 99 47
32 22 29 33 25
Left Side< 45 < Right
Side
67 76 98 99 47
45
32 22 29 33 25
Apply Recursive Partitioning on
Left Side
32 22 29 33 25
Apply Recursive Partitioning on Right
Side
67 76 98 99 47