Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Chapter3-StacksandQueues/3.5-QueueWithTwoStacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Problem: Impletement a MyQueue class which implements a queue using two stacks
*/

var MyQueue = function(){
this.current = [];
this.empty = [];
};

MyQueue.prototype.enqueue = function(val){
this.current.push(val);
};
//runtime O(1)

MyQueue.prototype.dequeue = function(){
var currentVal;
while (this.current.length > 1) {
this.empty.push(this.current.pop());
}
var result = this.current.pop();
while (this.empty.length > 0) {
this.current.push(this.empty.pop());
}
return result;
};
//runtime O(n)
12 changes: 12 additions & 0 deletions Sorting/SelectionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function selectionSort(array) {
for (var i = 0; i < array.length; i++) {
var min = i;
for (var k = i + 1; k < array.length; k++) {
if (array[k] < array[min]) min = k;
}
var temp = array[i];
array[i] = array[min];
array[min] = temp;
}
return array;
}
48 changes: 48 additions & 0 deletions Sorting/heapSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var Heap = function(){
this.heap = [];
}

Heap.prototype.insert = function(data) {
if (!this.heap.length) {
this.heap.push(data);
}
else {
var i = this.heap.length;
var parent = Math.floor(i/2);
this.heap[i] = data;
while (data > this.heap[Math.floor(i/2)] && i > 0){
var temp = this.heap[Math.floor(i/2)];
this.heap[ Math.floor(i/2)] = data;
this.heap[i] = temp;
i = Math.floor(i/2);
}
}
};

Heap.prototype.remove = function(){
var maximum = this.heap[0];
var n = this.heap.pop();
if (n === maximum) return n;
this.heap[0] = n;
var i = 0;
while (n < this.heap[2*i + 1] || n < this.heap[2*i + 2]){
var largest = this.heap[2*i +1] > (this.heap[2 * i + 2] || 0) ? 2*i + 1 : 2*i + 2;
this.heap[i] = this.heap[largest];
this.heap[largest] = n;
i = largest;
}
return maximum;
};

function heapSort(array) {
var heap = new Heap();
var result = [];
while (array.length > 0) {
var n = array.pop();
heap.insert(n);
}
while (heap.heap.length > 0) {
result.push(heap.remove());
}
return result;
}
23 changes: 23 additions & 0 deletions Sorting/mergeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function mergeSort(array) {
if (array.length === 1 || array.length === 0) return array;
else {
var midpoint = Math.floor(array.length / 2);
return merge(mergeSort(array.slice(0, midpoint)), mergeSort(array.slice(midpoint, array.length)));
}
}

function merge(array1, array2) {
var result = [];
var i = 0;
var j = 0;
while (i < array1.length || j < array2.length) {
if (array1[i] >= (array2[j] || 0)) {
result.push(array1[i]);
i++;
} else if (array2[j] > (array1[i] ||0)) {
result.push(array2[j]);
j++;
}
}
return result;
}
37 changes: 37 additions & 0 deletions Sorting/quicksort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var partition = function(array, left, right) {
var midpoint = Math.ceil((right + left)/2);
var pivot = array[midpoint];
while (left < right) {
while (array[left] < pivot) {
left ++;
}

while (array[right] > pivot) {
right --;
}
if (left < right ) {

swap(array, left, right);
}
}
return left;
};

var swap = function(array, index1, index2) {
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
};

var quickSort = function(array, left, right) {
if (!left) left = 0;
if (!right) right = array.length - 1;
var pivot = partition(array, left, right);
if (left < pivot - 1) {
quickSort(array, left, pivot -1 );
}
if (right > pivot + 1) {
quickSort(array, pivot, right);
}
return array;
};