Algorithm Visualizations
Cycle sort: Rotate permutation cycles into their final positions while handling duplicate values. Explore a worked example, complexity and tested code.
javascript
function algorithm(a, n) {
for (let begin = 0; begin < n - 1; begin++) {
let item = a[begin]; let pos = begin;
for (let i = begin + 1; i < n; i++) { if (a[i] < item) { pos++; } }
if (pos == begin) { continue; }
while (a[pos] == item) { pos++; }
let t = a[pos]; a[pos] = item; item = t;
while (pos != begin) {
pos = begin;
for (let i = begin + 1; i < n; i++) { if (a[i] < item) { pos++; } }
while (a[pos] == item) { pos++; }
t = a[pos]; a[pos] = item; item = t;
Nov 16, 2026
javascript
Algorithm Visualizations
Pigeonhole sort: Count values in holes spanning the minimum-to-maximum input range, then collect them. Explore a worked example, complexity and tested code.
javascript
function algorithm(a, n) {
let minimum = a[0]; let maximum = a[0]; let holes = Array(256).fill(0);
for (let i = 0; i < n; i++) { if (a[i] < minimum) { minimum = a[i]; } if (a[i] > maximum) { maximum = a[i]; } }
for (let i = 0; i < n; i++) { holes[a[i] - minimum]++; }
let k = 0;
for (let i = 0; i <= maximum - minimum; i++) {
while (holes[i] > 0) { a[k++] = minimum + i; holes[i]--; }
}
return 0;
}
Nov 16, 2026
javascript
Algorithm Visualizations
Bucket sort: Distribute into ten buckets, sort each bucket by insertion, then concatenate. Explore a worked example, complexity and tested code.
javascript
function algorithm(a, n) {
let count = Array(256).fill(0); let start = Array(256).fill(0); let temp = Array(256).fill(0);
for (let i = 0; i < n; i++) { count[Math.floor(a[i] / 10)]++; }
for (let b = 1; b < 10; b++) { start[b] = start[b - 1] + count[b - 1]; }
for (let i = 0; i < n; i++) { let b = Math.floor(a[i] / 10); temp[start[b]++] = a[i]; }
let left = 0;
for (let b = 0; b < 10; b++) {
let end = left + count[b];
for (let i = left + 1; i < end; i++) {
let j = i;
while (j > left && temp[j - 1] > temp[j]) { let t = temp[j]; temp[j] = temp[j - 1]; temp[j - 1] = t; j--; }
}
Nov 16, 2026
javascript
Algorithm Visualizations
Prefix-function construction: Compute the longest proper prefix that is also a suffix of every string prefix. Explore a worked example, complexity and tested…
javascript
function algorithm(a, n, output) {
output[0] = 0;
for (let i = 1; i < n; i++) {
let j = output[i - 1];
while (j > 0 && a[i] != a[j]) { j = output[j - 1]; }
if (a[i] == a[j]) { j++; }
output[i] = j;
}
return 0;
}
Nov 16, 2026
javascript
Algorithm Visualizations
Z-function construction: Compute the longest prefix match starting at each position, reusing the current Z-box. Z[0] is defined as zero. Explore a worked…
javascript
function algorithm(a, n, output) {
for (let i = 0; i < n; i++) { output[i] = 0; }
let left = 0; let right = 0;
for (let i = 1; i < n; i++) {
if (i <= right) { output[i] = right - i + 1; if (output[i - left] < output[i]) { output[i] = output[i - left]; } }
while (i + output[i] < n && a[output[i]] == a[i + output[i]]) { output[i]++; }
if (i + output[i] - 1 > right) { left = i; right = i + output[i] - 1; }
}
return 0;
}
Nov 16, 2026
javascript