Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
59 lines
2.7 KiB
Markdown
59 lines
2.7 KiB
Markdown
# heap
|
|
|
|
Companion code for the ankurm.com article **Heap and PriorityQueue in Java: Top-K, Kth Largest, Merge K Sorted Lists and Running Median**.
|
|
|
|
A binary heap built on a plain array (sift up, sift down, bottom-up heapify), `java.util.PriorityQueue` and its traps,
|
|
heap sort, and ten interview problems, each cross-checked against a brute-force answer on seeded random input.
|
|
Every figure and console line in the article is quoted from a file in `output/`, which the JUnit tests write while asserting
|
|
the same results. There is no `docs/` folder: the explanations live in the article itself.
|
|
|
|
## Versions
|
|
|
|
| Component | Version |
|
|
|---|---|
|
|
| JDK | Temurin 25.0.5.1+2 (LTS) |
|
|
| JUnit | 6.1.3 |
|
|
| Maven | 3.9.x |
|
|
|
|
## Quickstart
|
|
|
|
```bash
|
|
cd heap
|
|
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
|
|
./scripts/run-all.sh # runs the tests and regenerates output/*.txt
|
|
```
|
|
|
|
The first Maven run must be online so the plugins land in the local cache. `23-jdk-source.txt` is read from
|
|
`$JAVA_HOME/lib/src.zip`, so a different JDK build can shift its line numbers.
|
|
|
|
## Source layout
|
|
|
|
```
|
|
src/main/java/com/ankurm/interviewlab/heap/
|
|
├── IntMinHeap.java array min-heap of ints (the teaching version)
|
|
├── BinaryHeap.java generic comparator heap with bottom-up heapify
|
|
├── HeapDryRun.java records every compare and swap
|
|
├── HeapSort.java in-place heap sort
|
|
├── PriorityQueueTour.java everyday PriorityQueue usage
|
|
├── Pitfalls.java iteration order, subtraction comparators, mutation, ties, exceptions
|
|
└── problems/ ten problems plus BruteForce.java
|
|
src/test/java/.../ tests that write output/ and assert it
|
|
```
|
|
|
|
## Captured output (`output/`)
|
|
|
|
| File | What it shows |
|
|
|---|---|
|
|
| `01`, `02` | dry runs of offer (sift up) and poll (sift down), matching `PriorityQueue.toString()` |
|
|
| `03-same-array-as-jdk.txt` | my heaps and the JDK keep an identical backing array over 100,000 operations |
|
|
| `04-heapify-vs-offer.txt` | comparisons to build a heap of 1,000,000 keys, four ways |
|
|
| `05-operation-costs.txt` | `compareTo` / `equals` calls for peek, offer, poll, contains, remove |
|
|
| `06` to `10` | PriorityQueue tour, iteration order, subtraction comparator, mutation and ties, exceptions |
|
|
| `11` to `13` | heap sort trace, comparisons vs `Arrays.sort`, instability |
|
|
| `14` to `21` | the ten problems |
|
|
| `15-top-k-comparisons.txt` | sort vs size-k heap vs heapify-and-poll, nine input/k combinations |
|
|
| `22-brute-force-cross-checks.txt` | all ten problems against brute force |
|
|
| `23-jdk-source.txt` | `grow`, `offer`, `siftUpComparable`, `heapify` from the JDK's own source |
|
|
|
|
Comparison counts are deterministic for these seeds; they are not timings.
|