Quick Sort

Explanation

Quicksort is a divide-and-conquer sorting algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.

Example Use Case
Let's say we want to sort the array [5, 2, 9, 1, 5, 6].

1.  Choose a pivot (e.g., 5). Elements are partitioned into [2, 1] (less than 5), [5] (equal to 5, the pivot), and [9, 6, 5] (greater than 5).
2. Recursively sort [2, 1] to get [1, 2].
3. Recursively sort [9, 6, 5] to get [5, 6, 9].
4. Concatenate: [1, 2] + [5] + [5, 6, 9] results in [1, 2, 5, 5, 6, 9].
Theoretical Deep Dive

Quicksort's average time complexity is O(n log n), making it efficient for large datasets. However, its worst-case time complexity is O(n^2), which occurs when the pivot is consistently the smallest or largest element. Randomly selecting the pivot helps avoid the worst-case scenario. Space complexity is typically O(log n) due to the recursive calls. Quicksort is an in-place sorting algorithm, meaning it requires minimal additional memory.