UNPKG

algs4js

Version:

Basic algorithms and data structures implemented with es6

157 lines (147 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Class MemorySort * * Implementation of the Quick Sort algorithm using the * Hoare partition algorithm. */ var MemorySort = function () { function MemorySort() { _classCallCheck(this, MemorySort); } _createClass(MemorySort, null, [{ key: "selectionSort", value: function selectionSort(arr) { for (var i = 0; i < arr.length - 1; i++) { var min = this.findMin(arr, i); if (arr[i] > arr[min]) { this.swap(arr, i, min); } } } }, { key: "insertionSort", value: function insertionSort(arr) { for (var i = 1; i < arr.length; i++) { var curr = arr[i]; var j = i; while (j > 0 && arr[j - 1] > curr) { arr[j] = arr[j - 1]; j--; } arr[j] = curr; } } }, { key: "quickSort", value: function quickSort(arr) { this.qSortRecursive(arr, 0, arr.length - 1); } }, { key: "qSortRecursive", value: function qSortRecursive(arr, left, right) { if (left >= right) { return; } var pivot = this.partition(arr, left, right); this.qSortRecursive(arr, left, pivot - 1); this.qSortRecursive(arr, pivot + 1, right); } }, { key: "partition", value: function partition(arr, left, right) { var mid = Math.floor(left + (right - left) / 2); var pivot = arr[mid]; this.swap(arr, left, mid); var i = left; var j = right + 1; while (i < j) { while (arr[++i] < pivot) {} while (arr[--j] > pivot) {} if (i < j) { this.swap(arr, i, j); } } this.swap(arr, left, j); return j; } }, { key: "mergeSort", value: function mergeSort(arr) { this.mSortRecursive(arr, 0, arr.length - 1); } }, { key: "mSortRecursive", value: function mSortRecursive(arr, left, right) { if (left >= right) { return; } var mid = Math.floor(left + (right - left) / 2); this.mSortRecursive(arr, left, mid); this.mSortRecursive(arr, mid + 1, right); this.merge(arr, left, mid, right); } }, { key: "merge", value: function merge(arr, left, mid, right) { var arrCopy = arr.slice(); var i = left; var j = mid + 1; var k = left; while (i <= mid && j <= right) { if (arrCopy[i] < arrCopy[j]) { arr[k] = arrCopy[i]; i++; } else { arr[k] = arrCopy[j]; j++; } k++; } // One of the two sub-arrays has been exhausted // Replace remaining entries with the other sub-array while (i <= mid) { arr[k] = arrCopy[i]; i++; k++; } while (j <= right) { arr[k] = arrCopy[j]; j++; k++; } } }, { key: "findMin", value: function findMin(arr, i) { var min = arr[i]; var minj = i; for (var j = i; j < arr.length; j++) { if (arr[j] < min) { min = arr[j]; minj = j; } } return minj; } /* eslint no-param-reassign: 0 */ }, { key: "swap", value: function swap(arr, i, j) { var tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } }]); return MemorySort; }(); var arr = [3, 1, 2, 5, 4]; console.log("Memory selection sorting array " + arr); MemorySort.quickSort(arr); console.log("After Memory selection sorting array " + arr); exports.default = MemorySort;