androidengineers.Book a session

Divide and Conquer

Case Studies: Merge Sort, Quick Sort, Binary Search

article20 minMedium

Similar recursion can hide different work

Merge sort, quicksort, and binary search all reduce a problem, but they recurse over different numbers of subproblems and perform different nonrecursive work.

AlgorithmSubproblemsAdditional workTypical bound
Merge sortBoth halvesLinear mergeΘ(n log n)
Balanced quicksortBoth partitionsLinear partitionΘ(n log n)
Binary searchOne halfConstant comparisonΘ(log n) worst case

Quicksort's partitions can be extremely uneven, producing quadratic work. Merge sort controls split sizes but pays for merging and buffer management. Binary search discards half the candidates because ordering proves they cannot contain the desired answer.

Worked comparison

For sixteen elements, binary search follows one path of shrinking intervals. Merge sort explores every leaf but spreads linear total work over each level. An extreme-pivot quicksort can instead partition lengths sixteen, fifteen, fourteen, and so on.

Exercise

Draw these three call structures and count visited subproblems. Then identify where sorted-input assumptions enter binary search's correctness proof.

Check: the recurrence T(n)=T(n/2)+1 cannot describe an algorithm that actually explores both halves.

Further reading: Merge sort · Quicksort

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Case Studies: Merge Sort, Quick Sort, Binary Search | Algorithms | Android Engineers