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
| //children are 2n + 1, 2 | |
| //parents are floor((n-1)/2) | |
| class MaxHeap { | |
| constructor() { | |
| this.data = new Array(10); | |
| this.size = 0; | |
| } | |
| insert(value) { |
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
| class Queue{ | |
| constructor(){ | |
| this.stack1 = []; | |
| this.stack2 = []; | |
| } | |
| enqueue(data){ | |
| while(this.stack1.length){ | |
| var temp = this.stack1.pop(); |
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
| class Stack{ | |
| constructor(length){ | |
| this.data = new Array(length); | |
| this.pointer = 0; | |
| } | |
| push(value){ | |
| if(this.pointer === this.data.length){ | |
| throw "Overflow" |
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 selectionSort(input){ | |
| var temp; | |
| for(var j = 0; j < input.length - 1; j++){ | |
| var min = j; | |
| for(var i = j+1; i < input.length; i++){ | |
| if(input[i] < input[min]){ | |
| min = i; | |
| } | |
| } | |
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 insertionSort(input){ | |
| var j; | |
| var temp; | |
| for(var i = 1; i < input.length; i++){ | |
| j = i; | |
| while(j > 0 && input[j-1] > input[j]){ | |
| temp = input[j]; | |
| input[j] = input[j-1]; | |
| input[j-1] = temp; | |
| j--; |
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 bubblesort(input){ | |
| var n = input.length; | |
| var swapped; | |
| var temp; | |
| do{ | |
| swapped = false; | |
| for(var i = 1; i < n; i++){ | |
| if(input[i - 1] > input[i]){ | |
| temp = input[i]; | |
| input[i] = input[i-1]; |
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 mergesort(input){ | |
| if(input.length === 1) | |
| return input; | |
| var input1 = mergesort(input.slice(0, input.length/2)); | |
| var input2 = mergesort(input.slice(input.length/2)); | |
| return merge(input1, input2); | |
| } |
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(input, low, high){ | |
| if(low < high){ | |
| var pivot = partition(input, low, high); | |
| quicksort(input, low, pivot-1) | |
| quicksort(input, pivot+1, high); | |
| } | |
| } | |
| function partition(input, low, high){ | |
| var pivot = input[high]; |
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
| class MaxHeap{ | |
| constructor(){ | |
| this.data = []; | |
| this.position = 1; | |
| } | |
| insert(value){ | |
| this.data[this.position] = value; |
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
| class MaxHeap{ | |
| constructor(){ | |
| this.data = []; | |
| this.position = 1; | |
| } | |
| insert(value){ | |
| this.data[this.position] = value; |
NewerOlder