heap sort javascript
var array_length;
/* to create MAX array */
function heap_root(input, i) {
var left = 2 * i + 1;
var right = 2 * i + 2;
var max = i;
if (left < array_length && input[left] > input[max]) {
max = left;
}
if (right < array_length && input[right] > input[max]) {
max = right;
}
if (max != i) {
swap(input, i, max);
heap_root(input, max);
}
}
function swap(input, index_A, index_B) {
var temp = input[index_A];
input[index_A] = input[index_B];
input[index_B] = temp;
}
function heapSort(input) {
array_length = input.length;
for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) {
heap_root(input, i);
}
for (i = input.length - 1; i > 0; i--) {
swap(input, 0, i);
array_length--;
heap_root(input, 0);
}
}
var arr = [3, 0, 2, 5, -1, 4, 1];
heapSort(arr);
console.log(arr);
Also in JavaScript:
- next.js google script
- data for bar plot json
- check if time is greater than javascript
- Can't find Node.js binary "node": path does not exist. Make sure Node.js is installed and in your PATH, or set the "runtimeExecutable" in your launch.json
- how to count click events javascript
- how to set time out in js
- transaction commit rollback nodejs
- arange
- javascript check if undefined or null or empty string
- vue for loop
- attempt to invoke virtual method 'android.graphics.drawable.drawable react native
- javascript backslash
- Find the stray number
- can we get the value of form control after disabling it angular
- redirect to another domain javascript
- javascript object keys foreach
- react eslint prettier
- save or update mongoose
- javascript redirect with extra url arguments
- how could you implement javascript into java
- javascript regex not in a set of characters
- get gcf javascriot
- toastify js
- perl json response to list all the files in a directory