From dc121a4006a885d29966c024cbb25bf091d1f778 Mon Sep 17 00:00:00 2001 From: Sebby1770 <274013474+Sebby1770@users.noreply.github.com> Date: Sat, 22 Aug 2026 15:38:48 +1000 Subject: [PATCH] feat: extract SortCore step generators, add 4 algorithms + complexity profiler (v6) - sorting-core.js: all 14 sorting algorithms as pure step generators (browser global SortCore + Node CJS), one implementation shared by the page, the tests, and the profiler - New algorithms: Comb, Gnome, Cycle (minimal writes), simplified TimSort - Complexity Profiler: benchmarks every algorithm at n=16..512, log-log chart + fitted empirical growth exponent vs theoretical class - SortRunner now animates the op stream instead of hardcoding algorithms - 47 sorting unit tests incl. stability proofs and cycle write bound - SW cache v6, README/CHANGELOG updates Co-Authored-By: Claude Fable 5 --- CHANGELOG.md | 24 ++ README.md | 30 +- index.html | 20 +- script.js | 754 ++++++++++++++++++------------------------ sorting-core.js | 614 ++++++++++++++++++++++++++++++++++ style.css | 25 +- sw.js | 5 +- tests/sorting.test.js | 152 +++++++++ 8 files changed, 1175 insertions(+), 449 deletions(-) create mode 100644 sorting-core.js create mode 100644 tests/sorting.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 965b5e5..681be1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,30 @@ All notable changes to Algo Lab (Algorithm Visualizer). +## [2026-08-22] — v6 Sorting Core + +### Added +- **Four new sorting algorithms**: Comb Sort, Gnome Sort, Cycle Sort (provably minimal + writes — great for the writes metric), and a simplified TimSort (insertion-sorted + minruns + merge passes with the already-ordered shortcut) +- **`sorting-core.js`** — every sorting algorithm now lives in one dependency-free + module as a pure step generator (works in the browser as `SortCore` and in Node via + `require`). The page, the tests, and the profiler all consume the same op stream. +- **Complexity Profiler** — one click benchmarks all 14 algorithms at + n = 16, 32, 64, 128, 256, 512 on the current dataset shape, draws a log-log chart, + and reports each algorithm's measured growth exponent next to its theoretical class +- **47 sorting unit tests** (`node --test tests/sorting.test.js`): correctness on every + dataset shape and edge case, stability proofs via tagged keys, cycle sort's ≤ n write + bound and zero-writes-on-sorted property, exact bubble counts on reversed input, and + exponent-estimator checks that separate the quadratic sorts from the linearithmic ones + +### Changed +- `SortRunner` no longer hardcodes algorithm logic — it animates the `SortCore` + operation stream (compare / swap / write / touch / counts / mark ops), which keeps + metrics, narration, heatmap, sound, and step mode identical across all algorithms +- Tournament, race mode, and quiz automatically include the four new algorithms +- Service worker cache bumped to v6 and now precaches `sorting-core.js` + ## [2026-08-20] — v5 Path Lab ### Added diff --git a/README.md b/README.md index 11a4d8a..e378a7c 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ An interactive teaching lab built with pure HTML, CSS, and vanilla JavaScript Two labs share the same chrome, theme, and sound toggle: -- **Sort Lab** (`index.html`) — 10 sorting algorithms +- **Sort Lab** (`index.html`) — 14 sorting algorithms + empirical complexity profiler - **Path Lab** (`pathfinding.html`) — 6 pathfinding algorithms + maze generators -![Algorithms](https://img.shields.io/badge/sort-10-blue) ![Pathfinding](https://img.shields.io/badge/path-6-indigo) ![PWA](https://img.shields.io/badge/PWA-offline-purple) +![Algorithms](https://img.shields.io/badge/sort-14-blue) ![Pathfinding](https://img.shields.io/badge/path-6-indigo) ![PWA](https://img.shields.io/badge/PWA-offline-purple) ## Running @@ -28,12 +28,16 @@ macOS: `open index.html` · Linux: `xdg-open index.html` Pathfinding algorithms and maze generators are covered by Node’s built-in test runner (no packages to install): ```bash -node --test tests/pathfinding.test.js +node --test tests/sorting.test.js tests/pathfinding.test.js ``` +Sorting coverage includes correctness on every dataset shape, stability proofs for the +stable algorithms (via tagged keys), cycle sort's ≤ n write bound, and sanity checks on +the complexity profiler's exponent estimator. + ## Sort Lab -### Algorithms (10) +### Algorithms (14) | Algorithm | Best | Average | Worst | Stable | In-place | Memory | |-----------|------|---------|-------|--------|----------|--------| @@ -47,6 +51,13 @@ node --test tests/pathfinding.test.js | Radix | O(nk) | O(nk) | O(nk) | Yes | No | O(n + k) | | Counting | O(n + k) | O(n + k) | O(n + k) | Yes | No | O(k) | | Cocktail Shaker | O(n) | O(n²) | O(n²) | Yes | Yes | O(1) | +| Comb | O(n log n) | O(n²/2ᵖ) | O(n²) | No | Yes | O(1) | +| Gnome | O(n) | O(n²) | O(n²) | Yes | Yes | O(1) | +| Cycle | O(n²) | O(n²) | O(n²) | No | Yes | O(1) | +| TimSort (simplified) | O(n) | O(n log n) | O(n log n) | Yes | No | O(n) | + +All fourteen are implemented once, as pure step generators in `sorting-core.js` — +the page animates the operation stream, tests and the profiler consume it headlessly. ### Dataset generators @@ -62,7 +73,10 @@ Random · Sorted · Nearly Sorted · Reversed · Few Unique · Sawtooth · Custo - Presentation Mode - Access heatmap + operations sparkline - Algorithm recommender -- Benchmark tournament (all 10) with comparison matrix +- Benchmark tournament (all 14) with comparison matrix +- **Complexity Profiler** — measures total operations at n = 16…512, plots every + algorithm on a log-log chart, and fits the empirical growth exponent (the slope) + so you can watch bubble sort measure ≈ n² while counting sort measures ≈ n¹ - Learning cards (trivia + use cases) - Run history (last 8, `localStorage` key `sortLabHistory`) - Share URL, CSV export, copy summary @@ -124,9 +138,11 @@ algorithm-visualiser/ ├── index.html # Sort Lab ├── pathfinding.html # Path Lab ├── style.css # Shared theme + path grid -├── script.js # Sort Lab app +├── script.js # Sort Lab app (rendering, metrics, chrome) +├── sorting-core.js # Sorting step generators + profiler (browser + Node) ├── pathfinding.js # Path Lab UI ├── pathfinding-core.js # Search + mazes (browser + Node) +├── tests/sorting.test.js ├── tests/pathfinding.test.js ├── sw.js # Service worker ├── manifest.json # PWA manifest @@ -135,7 +151,7 @@ algorithm-visualiser/ └── README.md ``` -`pathfinding-core.js` exports `PathCore` in the browser and `module.exports` in Node. +`sorting-core.js` and `pathfinding-core.js` export `SortCore` / `PathCore` in the browser and `module.exports` in Node. ## License diff --git a/index.html b/index.html index e7969df..04b3f19 100644 --- a/index.html +++ b/index.html @@ -54,6 +54,10 @@

Controls

+ + + + @@ -73,6 +77,10 @@

Controls

+ + + + @@ -137,7 +145,8 @@

Controls

- + +

Space pause · S start · G generate · R reset · Esc exit present

@@ -178,6 +187,14 @@

Tournament Leaderboard

+ +