Skip to content

Commit 3d8e36c

Browse files
committed
Update sorting algorithms
1 parent 6cd5634 commit 3d8e36c

1 file changed

Lines changed: 81 additions & 30 deletions

File tree

app/examples/sorting.recho.js

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,24 @@
1010

1111
/**
1212
* ============================================================================
13-
* = Sorting Algorithms =
13+
* = Sorting Algorithms Visualization =
1414
* ============================================================================
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+
*
1920
*/
2021

2122
recho.button("Run", run);
2223

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");
4731

4832
const [array, setArray] = recho.state(data());
4933

@@ -83,8 +67,75 @@ function visualize(array, sorter, label) {
8367
}
8468
echo.set("compact", true)(output);
8569
}
86-
}, 20);
70+
}, 100);
8771
echo.dispose(() => clearInterval(timer));
8872
}
8973

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+
90141
const d3 = recho.require("d3");

0 commit comments

Comments
 (0)