Example of Quick Sort
Given Array:[5, 3, 8, 4, 1, 12, 7]
Step 1: Choose a Pivot
Take last element as pivot → 7
Rearrange so:
-
Elements < 7 → Left
-
Elements > 7 → Right
After partition:[5, 3, 4, 1, 7, 8, 12]
Now 7 is in correct position.
Step 2: Apply Quick Sort to Left and Right Parts
Sorting Left Part: [5, 3, 4, 1]
Pivot = 1
Rearranged → [1, 3, 4, 5]
Now sort remaining [3, 4, 5]
Pivot = 5
Rearranged → [3, 4, 5]
Sort [3, 4]
Pivot = 4 → [3, 4]
Left part sorted → [1, 3, 4, 5]
Sorting Right Part: [8, 12]
Pivot = 12
Rearranged → [8, 12]
Right part sorted → [8, 12]
Final Sorted Array
[1, 3, 4, 5, 7, 8, 12]
