diff --git a/src/searching/binarysearch.js b/src/searching/binarysearch.js index efe6e28c..660ff265 100644 --- a/src/searching/binarysearch.js +++ b/src/searching/binarysearch.js @@ -1,9 +1,8 @@ (function (exports) { 'use strict'; - function id (val) { return val; } - function get (key) { return function (val) { return val[key]; }; } - + const id = (val) => val; + const get = (key) => (val) => val[key]; /** * Searches for specific element in a given array using * the binary search algorithm.

@@ -21,14 +20,14 @@ * @param {Number} value Value of the element which index should be found. * @returns {Number} Index of the element or -1 if not found. */ - function binarySearch(array, value, key) { + const binarySearch = (array, value, key) => { key = !key ? id : typeof key === 'string' ? get(key) : key; value = key(value); - var middle = Math.floor(array.length / 2); - var left = 0; - var right = array.length; + let middle = Math.floor(array.length / 2); + let left = 0; + let right = array.length; while (right >= left) { - var middleValue = key(array[middle]); + const middleValue = key(array[middle]); if (middleValue === value) { return middle; } else if (middleValue > value) { @@ -39,8 +38,7 @@ middle = Math.floor((left + right) / 2); } return -1; - } - + }; exports.binarySearch = binarySearch; })(typeof window === 'undefined' ? module.exports : window); diff --git a/src/searching/knuth-morris-pratt.js b/src/searching/knuth-morris-pratt.js index b4aeb85b..d395bd55 100644 --- a/src/searching/knuth-morris-pratt.js +++ b/src/searching/knuth-morris-pratt.js @@ -1,14 +1,17 @@ (function (exports) { 'use strict'; - var kmp = (function () { - function builtKMPTable(str) { - var res = []; - var len; - var front; - var end; - var found; - for (var i = 1; i <= str.length; i += 1) { + const kmp = (str, substr) => { + if (str === substr) { + return 0; + } + const builtKMPTable = (str) => { + const res = []; + let len; + let front; + let end; + let found; + for (let i = 1; i <= str.length; i += 1) { front = Math.max(1, i - ((res[i - 2] || 0) + 1)); end = Math.min(i - 1, (res[i - 2] || 0) + 1); found = false; @@ -25,7 +28,7 @@ res[i - 1] = len; } return res; - } + }; /** * Knuth–Morris–Pratt algorithm. Searches for the position of @@ -45,35 +48,29 @@ * where the specified substring occurs for the first * time, or -1 if it never occurs. */ - function indexOf(str, substr) { - if (str === substr) { - return 0; + + const table = builtKMPTable(substr); + let i = 0; + let j = 0; + while (i < str.length) { + if (str[i] === substr[j]) { + i += 1; + j += 1; } - var table = builtKMPTable(substr); - var i = 0; - var j = 0; - while (i < str.length) { - if (str[i] === substr[j]) { + if (j === substr.length) { + return i - j; + } + if (i < str.length && str[i] !== substr[j]) { + if (j > 0 && table[j - 1] !== 0) { + j = table[j - 1]; + } else { i += 1; - j += 1; - } - if (j === substr.length) { - return i - j; - } - if (i < str.length && str[i] !== substr[j]) { - if (j > 0 && table[j - 1] !== 0) { - j = table[j - 1]; - } else { - i += 1; - j = 0; - } + j = 0; } } - return -1; } - return indexOf; - }()); - + return -1; + }; exports.kmp = kmp; - })(typeof window === 'undefined' ? module.exports : window); +