Files
java-interview-lab/merge-sort/README.md
T

75 lines
4.0 KiB
Markdown

# merge-sort
Companion code for the ankurm.com article **[Merge Sort in Java: Recursive and Bottom-Up, Dry Run, Complexity and Counting Inversions](https://ankurm.com/implementing-merge-sort-algorithm-in-java/)**
(an in-place rewrite of the 2015 post "Implementing Merge Sort Algorithm in Java").
Top-down and bottom-up merge sort, dry runs, the one character that decides stability, comparison counts against the exact worst-case formula,
merge sort on linked lists, counting inversions, a comparison with quick sort and `Arrays.sort`, and the 2015 program run again: it sorts correctly,
but is not stable on ties and allocates a full-size array in every merge (6,400 MB for 40,000 ints). Every figure and console line in the article is
quoted from a file in `output/`, which the JUnit tests write while asserting the numbers. There is no `docs/` folder: the explanations live in the article.
## Versions
| Component | Version | Verified against |
|---|---|---|
| JDK | Temurin 25.0.5.1+2 (LTS) | `java -version` |
| JUnit | 6.1.3 | `junit-bom` on Maven Central |
| Maven | 3.9.x | `mvn -version` |
## Quickstart
```bash
cd merge-sort
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
./scripts/run-all.sh # runs 17 tests and regenerates output/*.txt
java download/MergeSort.java # the single-file program from the article (type the numbers in)
java legacy/MergeSort.java # the original 2015 program, unchanged
```
The first Maven run must be online so the plugins land in the local cache.
## Source layout
```
merge-sort/
├── pom.xml
├── scripts/run-all.sh
├── download/MergeSort.java single-file program, also attached to the article
├── legacy/MergeSort.java the 2015 program, unchanged
├── src/main/java/com/ankurm/interviewlab/mergesort/
│ ├── TopDown.java recursive merge sort, one shared buffer
│ ├── BottomUp.java iterative: runs of width 1, 2, 4, ...
│ ├── SkipSorted.java skips merges whose runs are already in order
│ ├── ListMergeSort.java singly linked list, top-down and bottom-up
│ ├── BrokenSplit.java the wrong linked-list split, on purpose
│ ├── Inversions.java O(n log n) count plus the O(n^2) definition
│ ├── Stability.java generic version with the tie rule as a switch
│ ├── DryRun.java records every merge
│ ├── Measured.java counts comparisons (also a randomized quick sort)
│ └── legacy/Original2015.java the 2015 methods in a package, for tests
├── src/test/java/.../ tests that write output/
└── output/*.txt captured real output
```
## Captured output (`output/`)
| File | What it shows |
|---|---|
| `01-dry-run-top-down.txt` | the six merges on `[38, 27, 43, 3, 9, 82, 10]` |
| `02-dry-run-final-merge.txt` | the last merge, element by element |
| `03-dry-run-bottom-up.txt` | the same array, pass by pass |
| `04-stability.txt` | `<=` vs `<` on records with equal keys |
| `05-original-2015-program.txt` | the 2015 program with its sample input |
| `06-original-allocation.txt` | `new int[arr.length]` per merge, measured |
| `07-comparison-counts.txt` | comparisons on random, sorted, reversed and worst-case input |
| `08-brute-force.txt` | 488,281 exhaustive + 20,000 random arrays, 0 disagreements |
| `09-linked-list.txt` | relinking nodes, 1,000,000 nodes, the broken split |
| `10-inversions.txt` | inversion counts, `int` overflow, the wrong tie rule |
| `11-merge-vs-quick.txt` | comparisons against randomized quick sort |
| `12-jdk-arrays-sort.txt` | what `Arrays.sort` calls, read from `lib/src.zip` |
| `13-midpoint.txt` | `(lo + hi) / 2` overflow |
| `14-timsort-comparisons.txt` | comparisons of `Arrays.sort(Integer[], c)` on ordered input |
| `15-single-file-sessions.txt` | the single-file program with typed input |
Comparison counts are deterministic. Allocated-byte figures in `06` can move by a fraction of a percent between JVM builds.