# quick-sort Companion code for the ankurm.com article **Quick Sort in Java: Lomuto vs Hoare Partition, Dry Run and Complexity** (a full rewrite of three 2015 posts — "Implementing Quick Sort in Java" and the two "Implementing Quicksort Algorithm in Java" posts — now one article at the first URL). The Lomuto and Hoare partitions side by side, step-by-step dry runs, comparison/swap/depth counts for sorted, reverse, all-equal and few-distinct-key input, random pivots, three-way partitioning, recursing into the smaller side, and the three 2015 programs run again: one corrupts arrays that contain duplicates, one throws `ArrayIndexOutOfBoundsException`. Every figure and console line in the article is quoted from a file in `output/`, which the JUnit tests write while asserting the same numbers. There is no `docs/` folder: the explanations live in the article itself. ## Versions | Component | Version | Verified against | |---|---|---| | JDK | Temurin 25.0.5.1+2 (LTS) | `java -version` | | JUnit | 6.1.3 | `junit-bom` `maven-metadata.xml` on Maven Central (latest GA) | | Maven | 3.9.x | `mvn -version` | ## Quickstart ```bash cd quick-sort export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH" ./scripts/run-all.sh # runs 16 tests and regenerates output/*.txt java download/QuickSort.java # the single-file program from the article (type the numbers in) ``` The first Maven run must be online so the plugins land in the local cache. ## Source layout ``` quick-sort/ ├── pom.xml ├── scripts/run-all.sh ├── download/QuickSort.java the single-file program, also attached to the article ├── src/main/java/com/ankurm/interviewlab/quicksort/ │ ├── Lomuto.java last-element pivot, one scanning pointer │ ├── Hoare.java middle-element pivot, two pointers walking inwards │ ├── ThreeWay.java Dijkstra's < == > partition │ ├── Randomized.java Lomuto with a random pivot │ ├── StackSafe.java recurse into the smaller side, loop on the larger │ ├── Measured.java instrumented copies that count comparisons, swaps and depth │ ├── DryRuns.java instrumented copies that record every step │ ├── Pitfalls.java two plausible-looking wrong Hoare variants │ └── legacy/ the three 2015 programs, logic unchanged ├── src/test/java/.../ one test class per group of output files └── output/*.txt captured real output ``` ## Captured output (`output/`) | File | What it shows | |---|---| | `01-dry-run-lomuto.txt` | Lomuto on `[15, 34, 26, 67, 95, 24]`, every compare and swap | | `02-dry-run-hoare.txt` | Hoare on the same array: 5 partition calls instead of 3 | | `03-both-on-the-2015-samples.txt` | both schemes on the sample inputs of the other two 2015 posts | | `04-legacy-temp-array.txt` | the 2015 temp-array version turning `[3, 1, 3, 2]` into `[1, 2, 3, 0]` | | `05-legacy-first-pivot.txt` | the 2015 first-pivot version throwing on `[3, 1, 2]`; 4,680 of 10,000 random arrays | | `06-worst-case.txt` | four schemes x five inputs, n = 5,000: comparisons, swaps, depth | | `07-lomuto-vs-hoare-random.txt` | Hoare does fewer swaps but more comparisons on random input | | `08-randomized-pivot.txt` | sorted input, 20 seeds: 67,134 to 85,451 comparisons instead of 12,497,500 | | `09-three-way-duplicates.txt` | 20,000 keys from 10 distinct values, three schemes | | `10-stack-depth.txt` | `StackOverflowError` on sorted input vs recursion depth 2 when recursing into the smaller side | | `11-random-agreement.txt` | seven versions x 20,000 seeded arrays vs `Arrays.sort`, 0 disagreements | | `12-pitfalls.txt` | two Hoare mistakes that look fine, reproduced | | `13-jdk-source.txt` | what `Arrays.sort(int[])` and `sort(Object[])` call, read from `lib/src.zip` | | `14-single-file-sessions.txt` | `java download/QuickSort.java` with the inputs of the 2015 posts |