Skip to content

Instantly share code, notes, and snippets.

View brianxautumn's full-sized avatar

Brian brianxautumn

View GitHub Profile
//children are 2n + 1, 2
//parents are floor((n-1)/2)
class MaxHeap {
constructor() {
this.data = new Array(10);
this.size = 0;
}
insert(value) {
class Queue{
constructor(){
this.stack1 = [];
this.stack2 = [];
}
enqueue(data){
while(this.stack1.length){
var temp = this.stack1.pop();
class Stack{
constructor(length){
this.data = new Array(length);
this.pointer = 0;
}
push(value){
if(this.pointer === this.data.length){
throw "Overflow"
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;
}
}
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--;
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];
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);
}
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];
class MaxHeap{
constructor(){
this.data = [];
this.position = 1;
}
insert(value){
this.data[this.position] = value;
class MaxHeap{
constructor(){
this.data = [];
this.position = 1;
}
insert(value){
this.data[this.position] = value;