-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
56 lines (44 loc) · 1.2 KB
/
main.js
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
function shiftDown(arr, len, idx) {
let left = 2 * idx + 1,
right = 2 * idx + 2,
largest = idx;
if (left < len && arr[largest] < arr[left]) {
largest = left;
}
if (right < len && arr[largest] < arr[right]) {
largest = right;
}
if (idx != largest) {
[arr[idx], arr[largest]] = [arr[largest], arr[idx]];
shiftDown(arr, len, largest);
}
}
function heap_sort(arr) {
for (let i = Math.floor(arr.length / 2); i >= 0; --i) {
shiftDown(arr, arr.length, i);
}
for (let i = arr.length - 1; i > 0; --i) {
[arr[0], arr[i]] = [arr[i], arr[0]];
shiftDown(arr, i, 0);
}
}
function generateRandomArray(num, rangeFrom, rangeTo) {
if (rangeTo < rangeFrom) {
return;
}
const arr = new Array(num);
const range = rangeTo - rangeFrom + 1;
for (let i = 0; i < num; ++i) {
arr[i] = Math.floor(Math.random() * range + rangeFrom);
}
return arr;
}
const num = 20;
const rangeFrom = 1;
const rangeTo = 10000;
const arr = generateRandomArray(20, 1, 1000)
console.log("Original array:");
console.log(arr);
heap_sort(arr);
console.log("Sorted array:");
console.log(arr)