Skip to content

feat(sorting): add counting sort - #40

Open
bh-official wants to merge 1 commit into
salsadsid:mainfrom
bh-official:feat/counting-sort
Open

bh-official wants to merge 1 commit into
salsadsid:mainfrom
bh-official:feat/counting-sort

Conversation

@bh-official

Copy link
Copy Markdown

Closes #11

Adds counting sort to the sorting visualizer at /algorithms/sorting/counting-sort.

What changed

  • countingSort in src/lib/algorithms/sorting.js plus its SORTERS entry. It counts each value in a 100-slot array (one step per value, with v and count[v] shown as variables), then writes the values back in order (one step per write). Comparisons and swaps stay at 0 and writes end at n.
  • A new "write" bar role (roles.js) so written slots do not look like shifts.
  • Everything in the CONTRIBUTING checklist: code snippets in five languages, catalog page (insertion sort now links to it, removed from the planned list), explainer (includes when counting sort is a bad idea), comparison/learn copy, README table and roadmap, and three practice problems.
  • The sorting overview grid is now 2 columns on medium screens and 4 on large, since there are four sorts.
  • The "question only on the compare line" test now skips counting sort, because it has no comparisons and uses line 3 for i = 0.

Tests
New tests in tests/sorting.test.mjs: sorted output for random arrays, duplicates, single element, all equal, already sorted and reversed input; comparisons 0, swaps 0, writes == n; counting phase before write-back phase; no value lost or added. They failed before the change (no SORTERS.counting) and pass now.

Ran

  • npm test: 127 pass, 0 fail (119 before)
  • npm run lint: clean
  • npm run build: succeeds, /algorithms/sorting/counting-sort is generated

Notes

  • I built the page but did not look at it in a browser, so please check how the bars look, especially the new teal "Writing" colour.
  • complexity.stable is "N/A": the visualizer rewrites plain numbers, so stability cannot be observed, and the explainer FAQ says why. Easy to change to "Yes" if you prefer the textbook label.
  • The renderer is untouched. Counting steps reuse the key role while a value is read, the new write role for each write, and sorted for the filled prefix.

Add countingSort to the sorting visualizer: it tallies each value in a
100-slot count array, then writes the values back in order. Comparisons
and swaps stay at 0 and writes end at n.

Registered everywhere the contributing checklist lists: SORTERS, code
snippets in five languages, catalog page and roadmap, explainer,
comparison copy, README and practice problems. Adds a "write" bar role
and tests for the new sorter.

Closes salsadsid#11
@vercel

vercel Bot commented Oct 25, 2026

Copy link
Copy Markdown

@bh-official is attempting to deploy a commit to the salsadsid's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add counting sort to the sorting visualizer

1 participant