forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
58 lines (41 loc) · 1.23 KB
/
code.js
File metadata and controls
58 lines (41 loc) · 1.23 KB
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
49
50
51
52
53
54
55
56
57
58
tracer._print('Original array = [' + D.join(', ') + ']');
function heapSort(array, size) {
var i, j, temp;
for (i = Math.ceil(size / 2) - 1; i >= 0; i--) {
heapify(array, size, i);
}
for (j = size - 1; j >= 0; j--) {
temp = array[0];
array[0] = array[j];
array[j] = temp;
tracer._notify(0, j);
tracer._select(j);
heapify(array, j, 0);
tracer._print('Swapping elements : ' + array[0] + ' & ' + array[j]);
tracer._deselect(j);
}
}
function heapify(array, size, root) {
var largest = root;
var left = 2 * root + 1;
var right = 2 * root + 2;
var temp;
if (left < size && array[left] > array[largest]) {
largest = left;
}
if (right < size && array[right] > array[largest]) {
largest = right;
}
if (largest != root) {
temp = array[root];
array[root] = array[largest];
array[largest] = temp;
tracer._notify(largest, root);
tracer._print('Swapping elements : ' + array[root] + ' & ' + array[largest]);
heapify(array, size, largest);
}
}
tracer._sleep(1000);
tracer._pace(800);
heapSort(D, 10);
tracer._print('Final array = [' + D.join(', ') + ']');