├── .gitignore ├── LICENSE ├── README.md ├── exercises └── exercise237.js ├── search ├── binarySearch.js └── run.js └── sort ├── bubbleSort.js ├── insertionSort.js ├── mergeSort.js ├── run.js └── selectionSort.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Benjamin Winterberg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Algorithms 2 | ==================== 3 | 4 | I once again read the book [Introduction to Algorithms](http://mitpress.mit.edu/books/introduction-algorithms) and use this repo to implement some of the algorithms for the sake of fun. The code is written in JavaScript (ES6) and can be run from the terminal: 5 | 6 | ``` 7 | $ babel-node sort/run.js 8 | $ babel-node search/run.js 9 | ``` 10 | 11 | > Make sure you have [Node](https://nodejs.org/) and [Babel](https://babeljs.io/) installed. 12 | -------------------------------------------------------------------------------- /exercises/exercise237.js: -------------------------------------------------------------------------------- 1 | // Exercise 2.3-7 2 | // s is a set of positive integers (excluding zero). 3 | // check if two numbers exist in s which sum is equal to x 4 | function check(s, x) { 5 | let a = []; 6 | for (let i = 0; i < s.length; i++) { 7 | let v = s[i]; 8 | if (v < x) { 9 | a[v] = 1; 10 | } 11 | } 12 | 13 | let q = Math.floor(x / 2) - 1; 14 | for (let i = x-1; i >= q; i--) { 15 | let j = x - i; 16 | //console.log("i=%s; j=%s; x=%s", i, j, x); 17 | if (i !== j && a[i] === 1 && a[j] === 1) { 18 | return true; 19 | } 20 | } 21 | return false; 22 | } 23 | 24 | 25 | 26 | let s = [1, 3, 4, 5]; 27 | 28 | let test = (x) => console.log(`${x}: ${check(s, x)}`); 29 | 30 | for (let i = 1; i < 11; i++) { 31 | test(i); 32 | } 33 | -------------------------------------------------------------------------------- /search/binarySearch.js: -------------------------------------------------------------------------------- 1 | module.exports = function (v, a) { 2 | // assume a is sorted 3 | 4 | function binarySearch(v, a, i, j) { 5 | if (i === j) { 6 | return a[i] === v ? i : -1; 7 | } 8 | 9 | let k = Math.floor((j - i) / 2) + i; 10 | 11 | //console.log("binarySearch: i=%s; j=%s; k=%s; v=%s", i, j, k, v); 12 | 13 | if (a[k] === v) { 14 | return k; 15 | } 16 | 17 | if (a[k] < v) { 18 | return binarySearch(v, a, k + 1, j); 19 | } 20 | 21 | return binarySearch(v, a, i, k - 1); 22 | } 23 | 24 | return binarySearch(v, a, 0, a.length); 25 | }; 26 | -------------------------------------------------------------------------------- /search/run.js: -------------------------------------------------------------------------------- 1 | let binarySearch = require("./binarySearch.js"); 2 | 3 | let numbers = [0, 1, 2, 3, 4, 5, 6, 7]; 4 | 5 | let index = binarySearch(3, numbers); 6 | 7 | console.log("Index found:", index); 8 | -------------------------------------------------------------------------------- /sort/bubbleSort.js: -------------------------------------------------------------------------------- 1 | module.exports = function (a) { 2 | for (let i = 0; i < a.length; i++) 3 | for (let j = a.length; j > i; j--) 4 | if (a[j] < a[j-1]) 5 | [a[j], a[j-1]] = [a[j-1], a[j]]; 6 | }; 7 | -------------------------------------------------------------------------------- /sort/insertionSort.js: -------------------------------------------------------------------------------- 1 | module.exports = function insertionSort(a) { 2 | for (var j = 1; j < a.length; j++) { 3 | let val = a[j]; 4 | let i = j - 1; 5 | while (i >= 0 && a[i] > val) { 6 | a[i + 1] = a[i]; 7 | i -= 1; 8 | } 9 | a[i + 1] = val; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /sort/mergeSort.js: -------------------------------------------------------------------------------- 1 | function merge(a, p, q, r) { 2 | //console.log("p=%s; q=%s; r=%s", p, q, r); 3 | 4 | let n1 = q - p + 1; 5 | let n2 = r - q; 6 | let left = []; 7 | let right = []; 8 | 9 | for (let i = 0; i < n1; i++) { 10 | left[i] = a[p + i]; 11 | } 12 | 13 | for (let j = 0; j < n2; j++) { 14 | right[j] = a[q + j + 1]; 15 | } 16 | 17 | left.push(Number.MAX_VALUE); 18 | right.push(Number.MAX_VALUE); 19 | 20 | let i = 0; 21 | let j = 0; 22 | 23 | for (let k = p; k <= r; k++) { 24 | if (left[i] <= right[j]) { 25 | a[k] = left[i]; 26 | i++; 27 | } else if (right[j]) { 28 | a[k] = right[j]; 29 | j++; 30 | } 31 | } 32 | } 33 | 34 | function mergeSort(a, p, r) { 35 | if (p < r) { 36 | let q = Math.floor((p + r) / 2); 37 | mergeSort(a, p, q); 38 | mergeSort(a, q + 1, r); 39 | merge(a, p, q, r); 40 | } 41 | } 42 | 43 | module.exports = function (a) { 44 | mergeSort(a, 0, a.length - 1); 45 | }; 46 | -------------------------------------------------------------------------------- /sort/run.js: -------------------------------------------------------------------------------- 1 | let insertionSort = require("./insertionSort.js"); 2 | let selectionSort = require("./selectionSort.js"); 3 | let mergeSort = require("./mergeSort.js"); 4 | let bubbleSort = require("./bubbleSort.js"); 5 | 6 | 7 | let numbers = [8, 4, 2, 3, 1, 7, 3]; 8 | 9 | //insertionSort(numbers); 10 | //selectionSort(numbers); 11 | //mergeSort(numbers); 12 | bubbleSort(numbers); 13 | 14 | console.log("Result: ", numbers); 15 | -------------------------------------------------------------------------------- /sort/selectionSort.js: -------------------------------------------------------------------------------- 1 | module.exports = function selectionSort(a) { 2 | for (var j = 0; j < a.length - 1; j++) { 3 | let k = j; 4 | for (var i = j + 1; i < a.length; i++) { 5 | if (a[i] < a[k]) { 6 | k = i; 7 | } 8 | } 9 | let tmp = a[j]; 10 | a[j] = a[k]; 11 | a[k] = tmp; 12 | } 13 | }; 14 | --------------------------------------------------------------------------------