|
10 | 10 |
|
11 | 11 | /** |
12 | 12 | * ============================================================================ |
13 | | - * = Sorting Algorithms = |
| 13 | + * = Sorting Algorithms Visualization = |
14 | 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). |
| 15 | + * |
| 16 | + * This example demonstrates four sorting algorithms: Bubble Sort, Insertion |
| 17 | + * Sort, Selection Sort, and Quick Sort. Each algorithm is visualized with a |
| 18 | + * bar chart of the array elements. |
| 19 | + * |
19 | 20 | */ |
20 | 21 |
|
21 | 22 | recho.button("Run", run); |
22 | 23 |
|
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 | | -} |
| 24 | +visualize(array.slice(), sortBubble, "Bubble Sort"); |
| 25 | + |
| 26 | +visualize(array.slice(), sortInsertion, "Insertion Sort"); |
| 27 | + |
| 28 | +visualize(array.slice(), sortSelection, "Selection Sort"); |
| 29 | + |
| 30 | +visualize(array.slice(), sortQuick, "Quick Sort"); |
47 | 31 |
|
48 | 32 | const [array, setArray] = recho.state(data()); |
49 | 33 |
|
@@ -83,8 +67,75 @@ function visualize(array, sorter, label) { |
83 | 67 | } |
84 | 68 | echo.set("compact", true)(output); |
85 | 69 | } |
86 | | - }, 20); |
| 70 | + }, 100); |
87 | 71 | echo.dispose(() => clearInterval(timer)); |
88 | 72 | } |
89 | 73 |
|
| 74 | +function* sortSelection(array) { |
| 75 | + const n = array.length; |
| 76 | + yield array.slice(); |
| 77 | + for (let i = 0; i < n; i++) { |
| 78 | + let minIndex = i; |
| 79 | + for (let j = i + 1; j < n; j++) { |
| 80 | + if (array[j] < array[minIndex]) { |
| 81 | + minIndex = j; |
| 82 | + } |
| 83 | + } |
| 84 | + [array[i], array[minIndex]] = [array[minIndex], array[i]]; |
| 85 | + yield array.slice(); |
| 86 | + } |
| 87 | + yield array.slice(); |
| 88 | + return array; |
| 89 | +} |
| 90 | + |
| 91 | +function* sortInsertion(array) { |
| 92 | + const n = array.length; |
| 93 | + yield array.slice(); |
| 94 | + for (let i = 1; i < n; i++) { |
| 95 | + const current = array[i]; |
| 96 | + let j = i - 1; |
| 97 | + while (j >= 0 && array[j] > current) { |
| 98 | + array[j + 1] = array[j]; |
| 99 | + j--; |
| 100 | + } |
| 101 | + array[j + 1] = current; |
| 102 | + yield array.slice(); |
| 103 | + } |
| 104 | + yield array.slice(); |
| 105 | + return array; |
| 106 | +} |
| 107 | + |
| 108 | +function* sortBubble(array) { |
| 109 | + const n = array.length; |
| 110 | + yield array.slice(); |
| 111 | + for (let i = 0; i < n; i++) { |
| 112 | + for (let j = 0; j < n - i - 1; j++) { |
| 113 | + if (array[j] > array[j + 1]) { |
| 114 | + [array[j], array[j + 1]] = [array[j + 1], array[j]]; |
| 115 | + } |
| 116 | + } |
| 117 | + yield array.slice(); |
| 118 | + } |
| 119 | + yield array.slice(); |
| 120 | + return array; |
| 121 | +} |
| 122 | + |
| 123 | +function* sortQuick(array, left = 0, right = array.length - 1) { |
| 124 | + if (left >= right) return; |
| 125 | + |
| 126 | + let pivot = array[right]; // Pick the rightmost element as pivot. |
| 127 | + let i = left; |
| 128 | + for (let j = left; j < right; j++) { |
| 129 | + if (array[j] < pivot) { |
| 130 | + [array[i], array[j]] = [array[j], array[i]]; |
| 131 | + i++; |
| 132 | + } |
| 133 | + } |
| 134 | + [array[i], array[right]] = [array[right], array[i]]; |
| 135 | + yield array.slice(); |
| 136 | + |
| 137 | + yield* sortQuick(array, left, i - 1); |
| 138 | + yield* sortQuick(array, i + 1, right); |
| 139 | +} |
| 140 | + |
90 | 141 | const d3 = recho.require("d3"); |
0 commit comments