|
| 1 | +/** |
| 2 | + * @title Sorting Algorithms |
| 3 | + * @author Bairui Su |
| 4 | + * @created 2026-01-03 |
| 5 | + * @github pearmini |
| 6 | + * @pull_request 205 |
| 7 | + * @thumbnail_start 24 |
| 8 | + * @label Algorithm |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * ============================================================================ |
| 13 | + * = Sorting Algorithms = |
| 14 | + * ============================================================================ |
| 15 | + * Insertion sort is efficient for small arrays. Think of it like playing |
| 16 | + * cards: you're holding sorted cards in your left hand (sorted prefix/subarray) |
| 17 | + * and holding a card in your right hand (key). You insert the current card into |
| 18 | + * the sorted cards until there is no card in the table (unsorted subarray). |
| 19 | + */ |
| 20 | + |
| 21 | +recho.button("Run", run); |
| 22 | + |
| 23 | +//➜ Insertion Sort |
| 24 | +//➜ ▁ |
| 25 | +//➜ ▃▃▇█▃ ▃ █ |
| 26 | +//➜ ▂▃▃▆▆██████▆ █ ██ ▇ ▁ ▄ |
| 27 | +//➜ ▄▅████████████ █ ▅██▇█ █ ▂ █ |
| 28 | +//➜ ▁▂▂▃▇████████████████ █▃███████▆█▃█ |
| 29 | +//➜ ▁▄▅▆██████████████████████▄█████████████ |
| 30 | +visualize(array, insertionSort, "Insertion Sort"); |
| 31 | + |
| 32 | +function* insertionSort(array) { |
| 33 | + for (let i = 1; i < array.length; i++) { |
| 34 | + const key = array[i]; |
| 35 | + let j = i - 1; |
| 36 | + while (j >= 0 && array[j] > key) { |
| 37 | + array[j + 1] = array[j]; |
| 38 | + yield array.slice(); |
| 39 | + j--; |
| 40 | + } |
| 41 | + array[j + 1] = key; |
| 42 | + yield array.slice(); |
| 43 | + } |
| 44 | + yield array.slice(); |
| 45 | + return array; |
| 46 | +} |
| 47 | + |
| 48 | +const [array, setArray] = recho.state(data()); |
| 49 | + |
| 50 | +const chs = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]; |
| 51 | + |
| 52 | +function run() { |
| 53 | + setArray(data()); |
| 54 | +} |
| 55 | + |
| 56 | +function data() { |
| 57 | + return d3.range(40).map(() => d3.randomInt(1, 100)()); |
| 58 | +} |
| 59 | + |
| 60 | +function visualize(array, sorter, label) { |
| 61 | + const sortedArray = sorter(array); |
| 62 | + const y = d3.scaleLinear(d3.extent(array), [0, 40]); |
| 63 | + const timer = setInterval(() => { |
| 64 | + const step = sortedArray.next(); |
| 65 | + if (step.done) { |
| 66 | + clearInterval(timer); |
| 67 | + } else { |
| 68 | + echo.clear(); |
| 69 | + const len = chs.length; |
| 70 | + const Y = step.value.map(y).map((d) => { |
| 71 | + const n = Math.floor(d / len); |
| 72 | + const r = Math.floor(d - n * len); |
| 73 | + return chs[len - 1].repeat(n) + chs[r]; |
| 74 | + }); |
| 75 | + const maxHeight = d3.max(Y, (d) => d.length); |
| 76 | + const padY = Y.map((d) => d.padEnd(maxHeight, " ").split("")); |
| 77 | + let output = label ? `${label}\n` : ""; |
| 78 | + for (let i = 0; i < maxHeight; i++) { |
| 79 | + for (let j = 0; j < padY.length; j++) { |
| 80 | + output += padY[j][maxHeight - i - 1]; |
| 81 | + } |
| 82 | + output += i < maxHeight - 1 ? "\n" : ""; |
| 83 | + } |
| 84 | + echo.set("compact", true)(output); |
| 85 | + } |
| 86 | + }, 20); |
| 87 | + echo.dispose(() => clearInterval(timer)); |
| 88 | +} |
| 89 | + |
| 90 | +const d3 = recho.require("d3"); |
0 commit comments