Skip to content

Instantly share code, notes, and snippets.

@fronterior
Created May 19, 2022 04:39
Show Gist options
  • Save fronterior/06c41d8bf1dc04eb5dfbc0bcb38e88ef to your computer and use it in GitHub Desktop.
Save fronterior/06c41d8bf1dc04eb5dfbc0bcb38e88ef to your computer and use it in GitHub Desktop.
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