Arrays - Quick Sort
Quick Sort is a highly efficient sorting algorithm that employs a divide-and-conquer strategy. It divides the input array into smaller sub-arrays, recursively sorting them. It is commonly used because of its average-case efficiency on large datasets.
Speed:
How Quick Sort Works:
- Pivot Selection: Select a pivot element from the array.
- Partitioning: Rearrange the elements so that all elements smaller than the pivot are on its left, and all elements greater are on its right.
- Recursion: Recursively apply the same process to the sub-arrays formed by partitioning.
- Base Case: The recursion ends when the array is reduced to a single element or an empty sub-array.
Key Characteristics
- Time Complexity:
- Best Case:
- Average Case:
- Worst Case: (occurs when the pivot is the smallest or largest element)
- Space Complexity: due to the recursion stack.
- Stability: Quick Sort is not a stable sort (it does not preserve the relative order of equal elements).
- In-place Sort: It requires constant space, excluding the recursion stack.