-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.Heap Sort.html
64 lines (54 loc) · 1.63 KB
/
6.Heap Sort.html
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
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Heap Sort 堆排序</title>
</head>
<body>
<script>
// 堆排序也是一种很高效的算法,因其把数组当做二叉树来排序而得名。这个算法会根据以下信息,把数组当做二叉树来管理:
// 1.索引0是树的根节点
// 2.除根节点外,任意节点N的父节点是N/2;
// 3.节点L的左节点是2*L
// 4.节点R的右节点是2*R+1
var array = [3, 5, 1, 6, 4, 7, 2];
var heapSort = function(){
var heapSize = array.length;
buildHeap(array);
while(heapSize > 1){
heapSize--;
swap(array, 0, heapSize);
heapify(array, heapSize, 0);
}
}
var buildHeap = function(array){
var heapSize = array.length;
for(var i = Math.floor(array.length / 2); i >= 0; i--){
heapify(array, heapSize, i);
}
}
var heapify = function(array, heapSize, i){
var left = i * 2 + 1,
right = i * 2 + 2,
largest = i;
if(left < heapSize && array[left] > array[largest]){
largest = left;
}
if(right < heapSize && array[right] > array[largest]){
largest = right;
}
if(largest !== i){
swap(array, i, largest);
heapify(array, heapSize, largest);
}
}
var swap = function(array, index1, index2){
[array[index1], array[index2]] = [array[index2], array[index1]];
}
heapSort();
console.log(array);
</script>
</body>
</html>