Last active
August 22, 2020 08:03
-
-
Save abiodun0/9df6b1010c464d5421dbdea5e5548cfb to your computer and use it in GitHub Desktop.
Heap, max heap and heap sort,merge and quck sort
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
function heapify(arr, i) { | |
let [index, left, right] = [i, i*2+1, i*2+2] | |
if(arr[left] && arr[left] > arr[i]) { | |
index = left | |
} | |
if(arr[right] && arr[right] > arr[index]) { | |
index = right | |
} | |
if(index !== i) { | |
let temp = arr[i] | |
arr[i] = arr[index] | |
arr[index] = temp | |
heapify(arr, index) | |
} | |
} | |
function heapsort(arr) { | |
for(i=0; i<=arr.length/2; i++) { | |
heapify(arr, i) | |
} | |
let new_array = [] | |
while(arr.length) { | |
let temp = arr[0] | |
arr[0] = arr[arr.length -1] | |
arr[arr.length-1] = temp | |
new_array.unshift(arr.pop()) | |
heapify(arr, 0) | |
} | |
return new_array | |
} | |
heapsort([11,55,99,88,900,100,2,5,6,18,12]) |
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
function heapify(arr, i) { | |
let index = i | |
let left = index*2 + 1 | |
let right = index*2 + 2 | |
if(arr[left] > arr[index]) { | |
index = left | |
} | |
if(arr[right] > arr[index]) { | |
index = right | |
} | |
if(index != i) { | |
let temp = arr[i] | |
arr[i] = arr[index] | |
arr[index] = temp | |
heapify(arr, index) | |
} | |
return arr | |
} | |
function heapsort(arr) { | |
let i = Math.floor(arr.length/2 - 1) | |
while(i>-1) { | |
heapify(arr, i) | |
i-- | |
} | |
let new_array = [] | |
while(arr.length) { | |
let temp = arr[0] | |
arr[0] = arr[arr.length - 1] | |
arr[arr.length-1] = temp | |
new_array.unshift(arr.pop()) | |
heapify(arr, 0) | |
} | |
return new_array | |
} | |
experiment = [6, 5, 3, 1, 8, 7, 2, 4] | |
heapsort(experiment) |
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
var a = [34, 203, 3, 746, 200, 984, 198, 764, 9]; | |
function mergeSort(arr) | |
{ | |
if (arr.length < 2) | |
return arr; | |
var middle = Math.floor(arr.length / 2); | |
var left = arr.slice(0, middle); | |
var right = arr.slice(middle, arr.length); | |
return merge(mergeSort(left), mergeSort(right)); | |
} | |
function merge(left, right) | |
{ | |
var result = []; | |
while (left.length && right.length) { | |
if (left[0] <= right[0]) { | |
result.push(left.shift()); | |
} else { | |
result.push(right.shift()); | |
} | |
} | |
while (left.length) | |
result.push(left.shift()); | |
while (right.length) | |
result.push(right.shift()); | |
return result; | |
} |
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
function quicksort(arr) { | |
if(arr.length === 0) { | |
return arr | |
} | |
elm = arr[0] | |
return [].concat(arr.filter(x => x < elm)).concat(elm).concat(arr.fliter(x => x > elm)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment