Overview
Musser’s hybrid IntroSort (median-of-three 3-way QuickSort + depth-bounded HeapSort fallback + small-subarray InsertionSort) paired with linear-time 3-way Quickselect.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Cache-Friendly Subarray Insertion Sort
- Core Mechanical Concept: Low-Overhead O(N^2) Base Case for Small Partitions (N <= 16)
- Target Module:
index.js(insertionSortRange) - Engineering Specification: Implement
insertionSortRange(arr, lo, hi, cmp)to sortarr[lo..hi]in place. For subarrays of length $\le 16$, InsertionSort beats recursive QuickSort due to zero recursion overhead and sequential L1 cache hits. - Verification Invariant: sorts bounded subarray ranges in place via insertion sort
Step 2: In-Place Subarray HeapSort Fallback
- Core Mechanical Concept: Guaranteeing O(N log N) Worst-Case When Recursion Depth Exhausts
- Target Module:
index.js(heapSortRange) - Engineering Specification: Implement
heapSortRange(arr, lo, hi, cmp)over offsetloand lengthn = hi - lo + 1. Build a max-heap in $O(n)$ using Floyd’s bottom-upsiftDownfrom(n >> 1) - 1down to0, then repeatedly swaparr[lo]witharr[lo + end]andsiftDown(0, end). - Verification Invariant: sorts subarrays in O(N log N) worst-case via in-place HeapSort
Step 3: Median-of-Three & Dijkstra 3-Way Partition
- Core Mechanical Concept: Collapsing Duplicate Keys in O(N) via [ < pivot | == pivot | > pivot ]
- Target Module:
index.js(partition3Way) - Engineering Specification: Implement
partition3Way(arr, lo, hi, cmp)returning[lt, gt]. Maintain[lo..lt-1] < pivot,[lt..i-1] == pivot,[i..gt] unclassified, and[gt+1..hi] > pivot. Wheni > gt, return[lt, gt]. - Verification Invariant: partitions duplicate-heavy inputs into 3 regions and runs full IntroSort
Step 4: Linear-Time 3-Way Quickselect
- Core Mechanical Concept: Pruning One Partition per Step to Find the k-th Order Statistic in O(N)
- Target Module:
index.js(quickselect) - Engineering Specification: Implement
quickselect(arr, k, cmp)usingpartition3Way. Ifk >= lt && k <= gt,work[k]is in the equal-pivot band—return it immediately! Otherwise recurse/loop into[lo, lt - 1]ifk < lt, or[gt + 1, hi]ifk > gt. - Verification Invariant: selects exact k-th order statistics in expected O(N) time
Asymptotic & Memory Layout Guarantees
- Category: Sorting & Order Statistics
- Difficulty: Intermediate (~30 minutes)
- Key Architectural Highlights:
• Musser IntroSort combining 3-way Dutch National Flag QuickSort, HeapSort, and InsertionSort
• Depth trap 2*floor(log2 N) guaranteeing worst-case O(N log N) even on killer inputs
• Expected O(N) Quickselect with 3-way partitioning for duplicate-heavy arrays - Tags & Primitives:
IntroSort,Dijkstra 3-Way Partition,HeapSort Fallback,InsertionSort,Quickselect
Production Systems Context & Verification
This from-scratch implementation is engineered with zero external dependencies and deterministic verification harnesses. Every internal invariant—from pointer restoration and bitmask addressing to amortized potential bounds and tree height balance—is asserted directly in test.js. Use the interactive browser workbench above to complete the guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.