Created
May 19, 2022 04:39
-
-
Save fronterior/06c41d8bf1dc04eb5dfbc0bcb38e88ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const heap = Array.from(Array(100), () => Math.floor(Math.random() * 100)); | |
const main = () => { | |
for (let i = 1; i < heap.length; i++) run(heap, i); | |
}; | |
const run = (array, target) => { | |
const root = Math.floor((target - 1) / 2); | |
if (array[root] < array[target]) { | |
let temp = array[root]; | |
array[root] = array[target]; | |
array[target] = temp; | |
} | |
if (root) run(array, root); | |
} | |
main(); | |
console.log(heap); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment