-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHeap Sort.html
More file actions
39 lines (35 loc) · 912 Bytes
/
Heap Sort.html
File metadata and controls
39 lines (35 loc) · 912 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="SortTestHelper.js"></script>
<script>
function heapSort(arr) {
let len = arr.length;
for (let i = Math.floor((len - 1) / 2); i >= 0; i--) {
_shiftDown(arr, len, i)
}
for (let i = len - 1; i > 0; i--) {
swap(arr, i, 0);
_shiftDown(arr, i, 0);
}
console.log(arr)
}
function _shiftDown(arr, n, k) {
while (2 * k + 1 < n) {
let j = 2 * k + 1;
if (j + 1 < n && arr[j] > arr[j + 1]) {
j += 1
}
if (arr[k] <= arr[j]) break;
swap(arr, k, j);
k = j;
}
}
heapSort([32, 153, 100, -50, -10, 6, 5, 1356, 20, 160, 2, 1432, 4, 50, 14, 102, -30, 3, 45, 1312, 1, -1, -3])
</script>
</body>
</html>