Loading...
Quick Sort partitions the array in-place around a pivot establishing two subproblems separated by the pivot's final position. Average behavior yields O(n log n) work due to near-balanced splits in expectation; worst-case O(n^2) arises only with persistently extreme pivot placements.
General: T(n) = T(k) + T(n-k-1) + O(n) where k is pivot final index shift.
Average / Random: Expected k ≈ n/2 → T(n) ≈ 2T(n/2)+O(n) → O(n log n).
Worst: k ∈ {0, n-1} repeatedly → T(n)=T(n-1)+O(n)=O(n^2).
Depth: Balanced height ≈ log n; degenerate height n.
1function quickSort(arr, lo, hi):2 if lo >= hi: return3 p = partition(arr, lo, hi)4 quickSort(arr, lo, p-1)5 quickSort(arr, p+1, hi)