Quick Sort
📚 Quick Sort
Quick Sort is a divide-and-conquer algorithm.
It picks an element as a pivot and partitions the given array around the pivot such that:
-
Elements less than the pivot go to its left,
-
Elements greater than the pivot go to its right.
After partitioning, it recursively sorts the left and right parts.
⚡ Steps in Quick Sort
-
Choose a Pivot:
(You can choose first element, last element, middle element, or use a random pivot.) -
Partitioning:
Rearrange the array such that:-
All elements smaller than pivot come before it,
-
All elements greater come after it.
-
-
Recursively apply the same steps to left and right sub-arrays.
Quick Sort Algorithm (Pseudo code- first element as pivot)
✨ Example
Initial Array
Step 1: First Partition (pivot = 8)
-
Pivot = 8 (first element).
-
Elements ≤ 8 go left, elements > 8 go right.
After partition:
Pivot 8 is in its final position (index 5).
Step 2: Recurse on Left Part [2, 3, 1, 7, 0]
-
Pivot = 2.
Partition:
-
Left side ≤ 2 → [0, 1]
-
Right side > 2 → [7, 3]
After partition:
Step 3: Recurse on Left Part [0, 1]
-
Pivot = 0.
-
Partition: [0] [1]
Sorted result:
Step 4: Recurse on Right Part [7, 3]
-
Pivot = 7.
-
Partition: [3] [7]
Sorted result:
Step 5: Combine Left Side
Now left part [2, 3, 1, 7, 0] is fully sorted:
Step 6: Right Part [10]
Nothing to sort (single element).
Final Combined Sorted Array
Now combine everything:
✅ Final Answer:
The sorted array is:
Let’s do a complete step-by-step trace of Quick Sort on the array
using the last element (10) as the pivot in each recursive step.
✨ Example Initial Array ( last element as pivot)
Step 1: First Partition (pivot = 10)
-
Pivot = 10 (last element)
-
Compare each element with pivot.
i | Element | Comparison | Action |
---|---|---|---|
0 | 8 | 8 ≤ 10 | Stay left |
1 | 4 | 4 ≤ 10 | Stay left |
2 | 7 | 7 ≤ 10 | Stay left |
3 | 3 | 3 ≤ 10 | Stay left |
👉 All elements ≤ 10, so the pivot is already the largest.
After partition:
✅ Pivot 10 is now in correct position (index 4).
Left subarray: [8, 4, 7, 3]
Right subarray: (none)
Step 2: Recurse on Left Part [8, 4, 7, 3]
Pivot = 3 (last element)
i | Element | Comparison | Action |
---|---|---|---|
0 | 8 | 8 > 3 | stays right |
1 | 4 | 4 > 3 | stays right |
2 | 7 | 7 > 3 | stays right |
No element ≤ 3, so pivot goes to index 0 after swapping with first element.
After partition:
✅ Pivot 3 is in correct position (index 0).
Left subarray: (none)
Right subarray: [4, 7, 8]
Step 3: Recurse on Right Part [4, 7, 8]
Pivot = 8 (last element)
i | Element | Comparison | Action |
---|---|---|---|
1 | 4 | 4 ≤ 8 | Stay left |
2 | 7 | 7 ≤ 8 | Stay left |
All ≤ 8, so pivot stays at index 3.
After partition:
✅ Pivot 8 in correct position (index 3).
Left subarray: [4, 7]
Right subarray: (none)
Step 4: Recurse on Left Part [4, 7]
Pivot = 7
i | Element | Comparison | Action |
---|---|---|---|
1 | 4 | 4 ≤ 7 | Stay left |
No swaps needed. Pivot stays in correct position (index 2).
After partition:
✅ Pivot 7 in correct position.
Step 5: All Subarrays of Size 1
-
[3]
,[4]
,[7]
,[8]
,[10]
— already sorted.
✅ Final Sorted Array
🧠Summary of Recursive Steps
Step | Subarray | Pivot | Pivot Position | Resulting Array |
---|---|---|---|---|
1 | [8, 4, 7, 3, 10] | 10 | 4 | [8, 4, 7, 3, 10] |
2 | [8, 4, 7, 3] | 3 | 0 | [3, 4, 7, 8, 10] |
3 | [4, 7, 8] | 8 | 3 | [3, 4, 7, 8, 10] |
4 | [4, 7] | 7 | 2 | [3, 4, 7, 8, 10] |
🚀 Features of Quick Sort
Property | Value |
---|---|
Time Complexity | Best: O(n log n) |
Average: O(n log n) | |
Worst: O(n²) | |
Space Complexity | O(log n) (due to recursion) |
Type | In-place, Not Stable |
Comments
Post a Comment