In May 2021 I pushed my first Java project to GitHub: a window built with the NetBeans GUI Builder where you typed some numbers, picked a sorting algorithm, and it told you how many nanoseconds it took. I made it to practice Java after a long time with Python.
A while ago I opened it again. It had more bugs than I remembered: Merge Sort from a file didn't sort anything, Quick Sort dropped the last number, results were always saved to C:\Users\<user>\Desktop (goodbye macOS and Linux), and 50,000 already-sorted numbers crashed it with a StackOverflowError.
I fixed that version and kept it as is, as v1. Then I built a v2 from scratch in the same repo, so the difference is easy to see. It's called AlgoScope.
What it does
- Sorting: Bubble, Insertion, Selection, Merge, Quick and Heap Sort on bars. Random, already sorted, reversed or few-unique inputs, from 5 to 1,000 values.
- Pathfinding: BFS, DFS, Dijkstra and A* on a grid with walls and mud (stepping on mud costs 5, open ground costs 1). You can draw walls with the mouse, drag the start and the goal, or generate a maze.
- Full playback control: pause, one step forward, one step back, jump anywhere, and speed from 1 to ~30,000 steps per second.
- Live counters: comparisons, swaps and writes; explored cells, path steps and path cost.
The core idea: record once, replay however you want
Most visualizers I looked at put the animation inside the algorithm: compare, draw, Thread.sleep(), keep going. It works, but it mixes two jobs, needs a thread per run, and makes stepping back impossible.
In AlgoScope the algorithm runs once, all the way to the end, without knowing it's being watched. It only touches the data through an object that records everything it does. This is Bubble Sort:
public void sort(TrackedArray a) { for (int end = a.length() - 1; end > 0; end--) { boolean swapped = false; for (int j = 0; j < end; j++) { if (a.compare(j, j + 1) > 0) { a.swap(j, j + 1); swapped = true; } } if (!swapped) { return; } } } It's textbook Bubble Sort, with a.compare(j, j + 1) instead of a[j] > a[j + 1]. No threads, no sleeps, no UI code.
Every operation is stored as a step, and every step knows how to apply and undo itself. Java 21 made this really clean with a sealed interface and records:
public sealed interface SortStep extends Step<SortState> { record Compare(int i, int j) implements SortStep { ... } record Swap(int i, int j) implements SortStep { ... } record Write(int index, int oldValue, int newValue) implements SortStep { ... } } Write keeps the old value, so undoing it is trivial. And because the interface is sealed, the switches that pick a bar's color or describe the current step have no default: if I ever add a fourth kind of step, the code won't compile until every one of them handles it.
With that, a Timeline holds the state and the list of steps, and moving back is just this:
public boolean stepBack() { if (atStart()) { return false; } position--; steps.get(position).undo(state); return true; } Stepping back came for free. And everything became deterministic and easy to test without a screen, because a recording is just a list of values.
The biggest surprise: one tie-break made A* 15x faster
When I finished A*, I tried it on an open 30 x 30 grid with no walls. I expected it to go straight to the goal. It explored all 900 cells, exactly like Dijkstra.
The problem was the tie-break. On an open grid, lots of cells have exactly the same priority (cost so far + estimated distance to the goal). The priority queue returned them in arrival order, so A* finished every "layer" of equal priority before moving on, and on an open map that's the whole map.
The fix was one line in the comparator: on a tie, the cell closer to the goal goes first.
// Lowest priority first; on a tie, the one closer to the goal; then the oldest private static final Comparator<Entry> ORDER = Comparator .comparingInt(Entry::priority) .thenComparingInt(Entry::estimate) .thenComparingLong(Entry::order); | Tie-break | Cells explored (open 30 x 30) |
|---|---|
| Oldest first | 900 |
| Closest to the goal first | 59 |
The path costs the same either way; only the work changes. The last rule (arrival order) is there so every run is repeatable.
Another result I liked: on a random map with mud, BFS finds the path with the fewest steps (64), but it costs 140 because it walks straight through the mud. A* goes around it: 70 steps, cost 78, exploring half as many cells as Dijkstra. Watching it animated explains the difference better than any table.
344 tests, no JUnit
I wanted the project to need nothing but the JDK, even for tests. So I wrote a tiny runner: Check prints PASS/FAIL and AllTests exits with code 1 if anything fails. It runs on GitHub Actions on Windows, macOS and Linux.
Some of what's tested:
- Every sorting algorithm against
Arrays.sort: empty arrays, one element, duplicates, negatives,Integer.MIN_VALUE/MAX_VALUE, and Quick Sort on a million already-sorted values. - The counters against the theory: Bubble Sort on sorted input makes exactly n-1 comparisons; Selection Sort always compares every pair.
- Paths against a reference solver: Bellman-Ford, slow but too simple to get wrong, on 300 random maps (139 of them with no possible route).
- The UI without a screen: colors checked pixel by pixel in an image, buttons pressed with
doClick, and mouse drags simulated withMouseEvents.
And to test the tests, I planted bugs on purpose in a copy of the code: a reversed comparison, an undo that doesn't undo, an overestimating heuristic, A* without the tie-break... and checked that some test failed. That's how I found two gaps that became new tests.
v1 vs v2
| v1 (2021) | v2 AlgoScope | |
|---|---|---|
| What it does | Sorts what you type and shows the time | Animates every step, forwards and backwards |
| Algorithms | 5 sorting | 6 sorting + 4 pathfinding + a maze generator |
| What it measures | One time in nanoseconds (JIT noise included) | Comparisons, swaps, writes, cells and cost |
| UI | NetBeans GUI Builder, Windows-only fonts | Hand-written Swing, the same on every OS |
| Java | 8 | 21 (records, sealed, pattern matching) |
| Tests | 94 (added while fixing it) | 344 |
I decided not to build a timing benchmark. Measuring time properly in Java needs JIT warm-up and many runs, and the counters already tell the same O(n²) vs O(n log n) story without that noise: on 100 random values, Bubble Sort makes 4,935 comparisons and Merge Sort 596.
What I'm taking away
- Separating "what the algorithm does" from "how it's shown" was the best decision in the project. Everything else (stepping back, exact counters, tests without a screen) came from that.
- Small details matter. One line in a comparator was the difference between 900 and 59 cells.
- Keeping the old version is worth it. Seeing the 2021 code next to today's is the best way I have to measure how much I've grown.
To run it you only need JDK 21: inside the v2 folder, ./run.sh (or run.bat on Windows).
👉 Repo: github.com/Josafath/Sorting-Algorithms
👉 How it's built: ARCHITECTURE.md
👉 My portfolio: josafath-dev.vercel.app
If you have ideas for which algorithm to add next, or you'd do something differently, I'd love to read them in the comments.

