-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.js
48 lines (39 loc) · 1.17 KB
/
merge_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
36
37
38
39
40
41
42
43
44
45
46
47
48
function Merge() {
c_delay = 0;
merge_partition(0, array_size - 1);
enable_buttons();
}
function merge_sort(start, mid, end) {
var p = start,
q = mid + 1;
var Arr = [],
k = 0;
for (var i = start; i <= end; i++) {
if (p > mid) {
Arr[k++] = div_sizes[q++];
div_update(divs[q - 1], div_sizes[q - 1], "red"); //Color update
} else if (q > end) {
Arr[k++] = div_sizes[p++];
div_update(divs[p - 1], div_sizes[p - 1], "red"); //Color update
} else if (div_sizes[p] < div_sizes[q]) {
Arr[k++] = div_sizes[p++];
div_update(divs[p - 1], div_sizes[p - 1], "red"); //Color update
} else {
Arr[k++] = div_sizes[q++];
div_update(divs[q - 1], div_sizes[q - 1], "red"); //Color update
}
}
for (var t = 0; t < k; t++) {
div_sizes[start++] = Arr[t];
div_update(divs[start - 1], div_sizes[start - 1], "green"); //Color update
}
}
function merge_partition(start, end) {
if (start < end) {
var mid = Math.floor((start + end) / 2);
div_update(divs[mid], div_sizes[mid], "yellow"); //Color update
merge_partition(start, mid);
merge_partition(mid + 1, end);
merge_sort(start, mid, end);
}
}