From 85d2f180afef66d36890696edeb838e0a47e3009 Mon Sep 17 00:00:00 2001 From: KamillaKhabibrakhmanova Date: Tue, 26 Jul 2016 20:52:13 -0400 Subject: [PATCH] added basic sorting algorithms --- .../3.5-QueueWithTwoStacks.js | 26 ++++++++++ Sorting/SelectionSort.js | 12 +++++ Sorting/heapSort.js | 48 +++++++++++++++++++ Sorting/mergeSort.js | 23 +++++++++ Sorting/quicksort.js | 37 ++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 Chapter3-StacksandQueues/3.5-QueueWithTwoStacks.js create mode 100644 Sorting/SelectionSort.js create mode 100644 Sorting/heapSort.js create mode 100644 Sorting/mergeSort.js create mode 100644 Sorting/quicksort.js diff --git a/Chapter3-StacksandQueues/3.5-QueueWithTwoStacks.js b/Chapter3-StacksandQueues/3.5-QueueWithTwoStacks.js new file mode 100644 index 0000000..cad2971 --- /dev/null +++ b/Chapter3-StacksandQueues/3.5-QueueWithTwoStacks.js @@ -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) \ No newline at end of file diff --git a/Sorting/SelectionSort.js b/Sorting/SelectionSort.js new file mode 100644 index 0000000..ab6b8ac --- /dev/null +++ b/Sorting/SelectionSort.js @@ -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; +} \ No newline at end of file diff --git a/Sorting/heapSort.js b/Sorting/heapSort.js new file mode 100644 index 0000000..8e38014 --- /dev/null +++ b/Sorting/heapSort.js @@ -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; +} \ No newline at end of file diff --git a/Sorting/mergeSort.js b/Sorting/mergeSort.js new file mode 100644 index 0000000..071d181 --- /dev/null +++ b/Sorting/mergeSort.js @@ -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; +} \ No newline at end of file diff --git a/Sorting/quicksort.js b/Sorting/quicksort.js new file mode 100644 index 0000000..831531a --- /dev/null +++ b/Sorting/quicksort.js @@ -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; +}; \ No newline at end of file