Expected comparisons, from first principles

Randomized quicksort sorts 44 distinct keys by picking a uniformly random pivot, comparing every other key to it, recursing on the two parts. Derive the exact expected number of key comparisons (to four decimal places). Do not quote the 2nln⁡n2n\ln n asymptotic - compute the exact value for n=4n=4.

Show hints (2)+
  1. Ranks zi,zjz_i,z_j are compared iff the first pivot drawn from the block zi..zjz_i..z_j is one of the two endpoints - probability 2/(j−i+1)2/(j-i+1).
  2. Sum 2/(j−i+1)2/(j-i+1) over all pairs by rank-distance: 3(1)+2(23)+1(12)3(1)+2(\tfrac23)+1(\tfrac12).

Answer

Reveal answer →

4.8333 (± 0.001)

Create a free account to see the full worked solution and track your progress.

Interview format: Market Maker, Prop Trading

Related questions