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)
✨ Example
Sort: [8, 3, 1, 7, 0, 10, 2]
-
Choose pivot
2
(last element). -
Partition: elements <=2 on left, >2 on right →
[1, 0, 2, 7, 8, 10, 3]
-
Recursive calls:
-
Left:
[1, 0]
→ pivot0
→[0, 1]
-
Right:
[7,8,10,3]
→ pivot3
→[3,7,10,8]
-
Continue until fully sorted:
[0,1,2,3,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