How Quick Sort Works
Quick sort selects a pivot element and partitions the array so that elements less than the pivot come before it and elements greater come after. It then recursively sorts the partitions. In practice, it is one of the fastest general-purpose sorting algorithms.
Choose a pivot element, then rearrange the array so all elements smaller than the pivot are on its left, and all larger elements are on its right. Recursively apply the same process to the left and right partitions.
Complexity
- Time
- O(n log n)
- Space
- O(log n)
- Stability
- unstable
When to Use Quick Sort
- General-purpose sorting with excellent average performance
- In-memory sorting where cache performance matters
- Large datasets where average-case O(n log n) is acceptable