Searching and sorting · Algorithm 4 of 15
Quicksort
Partition around a pivot and see why balance determines cost. Citation needed
The whole, before the parts
Quicksort orders a collection by separating values around a chosen value, then sorting the smaller groups. Each separation establishes which groups belong before or after others in the final result. Citation needed
Terminology and core concepts
A pivot is a selected value used to divide a list into smaller, equal, and larger values. Recursively sorting the smaller and larger parts is enough: every smaller value belongs before the equal group, and every larger one belongs after it. This readable version uses three temporary lists and returns a new list. It chooses the middle position, which does not guarantee a median value or a balanced partition. Citation needed
Learning path
Trace an example
input [7, 2, 5, 2, 9] pivot = 5 partition [2, 2] | [5] | [7, 9] recurse [2, 2] | [5] | [7] [9] combine [2, 2, 5, 7, 9]
Implement it in Python
Read the loop alongside the trace, then run this self-contained example with Python 3. The assertions show expected results. Inputs use ordinary Python lists and dictionaries; comparisons and numeric operations are assumed to behave normally. Citation needed
def quicksort(values):
if len(values) <= 1:
return values[:]
pivot = values[len(values) // 2]
smaller = [x for x in values if x < pivot]
equal = [x for x in values if x == pivot]
larger = [x for x in values if x > pivot]
return quicksort(smaller) + equal + quicksort(larger)
assert quicksort([7, 2, 5, 2, 9]) == [2, 2, 5, 7, 9]
assert quicksort([]) == []
assert quicksort([3, 3, 3]) == [3, 3, 3]Citation needed
Analyze the cost
Balanced partitions yield O(n log n) time; repeatedly removing only one element yields O(n²). Randomly ordered distinct inputs give expected O(n log n) time. All-equal input takes O(n). This list-based version uses O(n) peak extra space when balanced but can retain O(n²) elements across a worst-case recursion chain. It is not the usual in-place quicksort. Citation needed
A mistake to watch for
A middle-position pivot is not necessarily the middle-ranked value. Deep recursion can exceed Python’s recursion limit; this is teaching code for small inputs. Citation needed
Practice and recall
-
What happens when every input value equals the pivot?
Work it out before opening the solution. Citation needed
Show the reasoning
- The smaller and larger lists are empty.
- All values enter the equal list in one partition pass.
- Both recursive calls stop immediately; total work is linear.
Citation needed
- What partition shape causes quadratic quicksort time?
- Repeatedly splitting into zero and n - 1 elements. Citation needed
To check your understanding, reproduce the implementation without looking, explain why its main step is safe, and test an edge case before moving to the next algorithm. Citation needed
Sources
- Princeton: Algorithms — sorting — further study of the underlying algorithm and analysis.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms — correctness arguments and complexity analysis.