Quick Sort Example
Example Array:
[9, 1, 8, 3, 7, 2, 15, 11, 6, 4]
We’ll do Quick Sort using the last element as pivot.
Step 1: Choose pivot
Pivot = 4 (last element)
Partition array so elements ≤ pivot go left, > pivot go right:
• Left: [1, 3, 2]
• Pivot: [4]
• Right: [9, 8, 7, 15, 11, 6]
Array now:
[1, 3, 2, 4, 9, 8, 7, 15, 11, 6]
Step 2: Recursively Quick Sort left [1, 3, 2]
Pivot = 2
• Left: [1]
• Pivot: [2]
• Right: [3]
Array now:
[1, 2, 3]
Step 3: Recursively Quick Sort right [9, 8, 7, 15, 11, 6]
Pivot = 6
• Left: []
• Pivot: [6]
• Right: [9, 8, 7, 15, 11]
Further sorting:
1. Pivot = 15 → Left: [9, 8, 7, 11], Pivot: 15, Right: []
2. Pivot = 11 → Left: [9, 8, 7], Pivot: 11, Right: []
3. Pivot = 7 → Left: [], Pivot: 7, Right: [9, 8]
4. Pivot = 8 → Left: [], Pivot: 8, Right: [9]
Sorted right:
[6, 7, 8, 9, 11, 15]
Step 4: Combine left, pivot, right
Left [1, 2, 3], Pivot [4], Right [6, 7, 8, 9, 11, 15]
Final Sorted Array:
[1, 2, 3, 4, 6, 7, 8, 9, 11, 15]