This commit is contained in:
2026-07-05 23:03:23 +02:00
parent b26c3d68a5
commit 5aca925a3e
20 changed files with 2578 additions and 1224 deletions
+32
View File
@@ -0,0 +1,32 @@
import { AlgorithmKey, ComplexityClass } from "./types";
export const complexityClass: Record<AlgorithmKey, ComplexityClass> = {
bubble: "n2",
insertion: "n2",
heap: "nlogn",
quick: "nlogn",
merge: "nlogn",
radix: "n+k",
counting: "n+k",
bucket: "n+k",
shell: "n2-adaptive",
};
// A tiny sample of points used to draw each growth curve on a shared
// 0..100 axis, normalized to the same visual scale so n^2 vs n log n vs
// n is instantly, viscerally comparable — the whole point of drawing it.
export function curvePoints(fn: (n: number) => number, maxN = 100, steps = 40) {
const raw = Array.from({ length: steps + 1 }, (_, i) => {
const n = (i / steps) * maxN;
return { n, v: fn(Math.max(n, 1)) };
});
const maxV = Math.max(...raw.map((p) => p.v));
return raw.map((p) => ({ n: p.n, v: p.v / maxV }));
}
export const complexityCurves: Record<ComplexityClass, (n: number) => number> = {
n2: (n) => n * n,
nlogn: (n) => n * Math.log2(Math.max(n, 2)),
"n+k": (n) => n,
"n2-adaptive": (n) => n * Math.log2(Math.max(n, 2)) * 1.3,
};