#separator:tab
#html:true
#notetype:Basic
#tags column:3
#deck:cracked.guide::TAOCP Vol 3::5.2.2 Sorting by Exchanging
#columns:Front	Back	Tags
TAOCP §5.2.2 Sorting by Exchanging: what does it cover?	Bubble sort and its analysis, Batcher's parallel merge exchange, quicksort with careful analysis of its average case, and radix exchange sort, which partitions on bits.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::about
TAOCP §5.2.2: why does it matter?	Quicksort is the most used sorting algorithm in the world. Knuth's analysis shows exactly why choosing the pivot and handling small subarrays matter.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::why
TAOCP §5.2.2: what traps should you watch for?	<ul><li>Quadratic behavior on sorted input with a naive pivot.</li><li>Partitioning bugs with equal keys, the most common real-world quicksort failure.</li><li>Unbounded recursion depth: recurse on the smaller side first.</li></ul>	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::traps
TAOCP §5.2.2, exercise 3 (rated 10): what is it asking?	Is bubble sort stable? Quick check of the definition.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 33 (rated 15, recommended): what is it asking?	Move all negative numbers ahead of the others with the fewest swaps. Partitioning in its simplest form.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 18 (rated 20, recommended): what is it asking?	What quicksort does when every key is equal, and what breaks if you loosen the comparisons. The bug that bites real implementations.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 31 (rated 20, recommended): what is it asking?	Turn quicksort into a selection algorithm for the m-th smallest. This is quickselect.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 55 (rated 22, recommended): what is it asking?	Add median-of-three pivot choice to the MIX quicksort.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 26 (rated M24, recommended): what is it asking?	Build an input that makes the MIX quicksort run as slowly as possible. Teaches what the worst case really looks like.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 41 (rated 30, recommended): what is it asking?	Three-way partitioning into less, equal and greater blocks. The fix for inputs with many duplicate keys.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2, exercise 38 (rated M30, recommended): what is it asking?	Average-case quantities for radix exchange on a second input model. The stretch, and it needs exercise 36.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercise
TAOCP §5.2.2: how many exercises, and how are they rated?	56 exercises, rated 10 to M48.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::exercises
TAOCP §5.2.2: what should you be comfortable with first?	<ul><li>5.2.1 Sorting by Insertion</li><li>1.2.7 Harmonic Numbers</li></ul>	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::prereqs
TAOCP §5.2.2: how should you work through it?	Implement quicksort carefully with Knuth's partitioning scheme, a cutoff to insertion sort, and median-of-three. Test on sorted input, reverse-sorted input and all-equal keys. Read the bubble sort analysis for technique; bubble sort itself is not worth using.	cracked.guide cracked.guide::phase::taocp-3 cracked.guide::section::5.2.2 cracked.guide::kind::approach
