Added Web-Programming (Open Weather Maps data fetch) (#196)

* Added Web-Programming (Open Weather Maps data fetch)

* update

Co-authored-by: itsvinayak <itssvinayak@gmail.com>
This commit is contained in:
Tapajyoti Bose
2020-06-21 22:08:29 +05:30
committed by GitHub
parent 78262df0a2
commit 412995ac09
3 changed files with 44 additions and 0 deletions

45
Sorts/HeapSortV2.js Normal file
View File

@ -0,0 +1,45 @@
let arrayLength = 0
/* to create MAX array */
function heapRoot (input, i) {
const left = 2 * i + 1
const right = 2 * i + 2
let max = i
if (left < arrayLength && input[left] > input[max]) {
max = left
}
if (right < arrayLength && input[right] > input[max]) {
max = right
}
if (max !== i) {
swap(input, i, max)
heapRoot(input, max)
}
}
function swap (input, indexA, indexB) {
[input[indexA], input[indexB]] = [input[indexB], input[indexA]]
}
function heapSort (input) {
arrayLength = input.length
for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
heapRoot(input, i)
}
for (let i = input.length - 1; i > 0; i--) {
swap(input, 0, i)
arrayLength--
heapRoot(input, 0)
}
}
const arr = [3, 0, 2, 5, -1, 4, 1]
heapSort(arr)
console.log(arr)