Why Interviewers Ask This

This is a classic screening question for Data Structures & Algorithms roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

Quicksort is a highly efficient divide-and-conquer sorting algorithm that picks a pivot element, partitions the array into elements less than, equal to, and greater than the pivot, then recursively sorts the sub-arrays. Partition step: rearrange so all elements < pivot come before it, all > pivot come after. Algorithm: (1) Pick a pivot (first, last, middle, or random); (2) Partition: put elements < pivot left, > pivot right; (3) Recursively sort left and right partitions. Time: O(n log n) average and best case, O(n²) worst case (when pivot is always the smallest/largest — e.g., sorted array with first-element pivot). Space: O(log n) average for call stack, O(n) worst. Stable: NO (standard implementation). Pivot selection: random pivot or median-of-three avoids O(n²) in practice. Practical performance: quicksort is generally fastest for in-memory sorting — excellent cache behavior, in-place, small constant factors. C++ std::sort, Python's Timsort (for primitives), Java's primitive arrays use quicksort-based algorithms. Three-way partitioning (Dutch National Flag) handles many equal elements efficiently: O(n) for all-same-element input. Introsort: quicksort that switches to heapsort when recursion depth exceeds O(log n) — avoids O(n²) worst case.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.