Skip to content

Commit 4d50aae

Browse files
committed
Add code and data
1 parent ff2c65b commit 4d50aae

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

algorithm/sorting/heap/basic/code.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
tracer._print('Original array = [' + D.join(', ') + ']');
2+
3+
function heapSort(array, size) {
4+
var i, j, temp;
5+
6+
for (i = Math.ceil(size / 2) - 1; i >= 0; i--) {
7+
heapify(array, size, i);
8+
}
9+
10+
for (j = size - 1; j >= 0; j--) {
11+
temp = array[0];
12+
array[0] = array[j];
13+
array[j] = temp;
14+
15+
tracer._select(j);
16+
17+
heapify(array, j, 0);
18+
tracer._print('Swapping elemnts : ' + array[0] + ' & ' + array[j]);
19+
20+
tracer._deselect(j);
21+
}
22+
}
23+
24+
function heapify(array, size, root) {
25+
26+
var largest = root;
27+
var left = 2 * root + 1;
28+
var right = 2 * root + 2;
29+
var temp;
30+
31+
if (left < size && array[left] > array[largest]) {
32+
largest = left;
33+
}
34+
35+
if (right < size && array[right] > array[largest]) {
36+
largest = right;
37+
}
38+
39+
if (largest != root) {
40+
temp = array[root];
41+
array[root] = array[largest];
42+
array[largest] = temp;
43+
44+
tracer._notify(largest, root);
45+
46+
tracer._print('Swapping elemnts : ' + array[root] + ' & ' + array[largest]);
47+
48+
heapify(array, size, largest);
49+
}
50+
}
51+
52+
tracer._sleep(1000);
53+
tracer._pace(800);
54+
55+
heapSort(D, 10);
56+
57+
tracer._print('Final array = [' + D.join(', ') + ']');

algorithm/sorting/heap/basic/data.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var tracer = new Array1DTracer();
2+
var D = Array1D.random(10);
3+
tracer._setData(D);

0 commit comments

Comments
 (0)