Math Fundamentals18 sections · 814 units
Open in Course

Logarithms in Quicksort (Average case)

Balanced partitions

Quicksort picks a pivot, partitions the array around it, then recursively sorts each partition. In the average case, the pivot splits the array roughly in half.

If partitions are balanced, the recursion tree has height O(logn)O(\log n), just like merge sort. Each level does O(n)O(n) work, giving O(nlogn)O(n \log n) average time.

But in the worst case (always picking the smallest or largest element as pivot), the tree has height O(n)O(n), giving O(n2)O(n^2) time. Randomization keeps the average case O(nlogn)O(n \log n).