-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathheapSort.jsx
More file actions
47 lines (42 loc) · 731 Bytes
/
heapSort.jsx
File metadata and controls
47 lines (42 loc) · 731 Bytes
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
//heap sort test
function hpSort(){
length = data.length - 1;
while (length > 0) {
mkHeap(0);
swap(0, length);
length--;
}
}
function mkHeap(i) {
var node1 = i * 2 + 1;
var node2 = i * 2 + 2;
if (node1 > length) {
return
}
if (node1 == length) {
if (data[node1] > data[i]) {
swap(i, node1);
}
return
}
mkHeap (node1);
mkHeap (node2);
if (data[node1] >= data[node2]) {
largeNode = node1;
} else {
largeNode = node2;
}
if (data[i] < data[largeNode]) {
swap (i, largeNode);
mkHeap (largeNode);
}
}
function swap(a, b){
var num = data[a];
data[a] = data[b];
data[b] = num;
}
data = new Array();
data =[2,5,8,4,21,15,16,11,10,6,9,7,17,1];
hpSort();
alert (data);