-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_sort.js
35 lines (28 loc) · 1.03 KB
/
insertion_sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Insertion() {
c_delay = 0;
for (var j = 0; j < array_size; j++) {
div_update(divs[j], div_sizes[j], "yellow"); //Color update
var key = div_sizes[j];
var i = j - 1;
while (i >= 0 && div_sizes[i] > key) {
div_update(divs[i], div_sizes[i], "red"); //Color update
div_update(divs[i + 1], div_sizes[i + 1], "red"); //Color update
div_sizes[i + 1] = div_sizes[i];
div_update(divs[i], div_sizes[i], "red"); //Height update
div_update(divs[i + 1], div_sizes[i + 1], "red"); //Height update
div_update(divs[i], div_sizes[i], "blue"); //Color update
if (i == j - 1) {
div_update(divs[i + 1], div_sizes[i + 1], "yellow"); //Color update
} else {
div_update(divs[i + 1], div_sizes[i + 1], "blue"); //Color update
}
i -= 1;
}
div_sizes[i + 1] = key;
for (var t = 0; t < j; t++) {
div_update(divs[t], div_sizes[t], "green"); //Color update
}
}
div_update(divs[j - 1], div_sizes[j - 1], "green"); //Color update
enable_buttons();
}