From e42a2265e0651a40c65f9829c4908eee2b053d13 Mon Sep 17 00:00:00 2001 From: King Date: Fri, 5 Jan 2018 10:32:10 -0500 Subject: [PATCH] add binarySearch --- snippets/binarySearch.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/binarySearch.md diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md new file mode 100644 index 000000000..0af272a6c --- /dev/null +++ b/snippets/binarySearch.md @@ -0,0 +1,23 @@ +### binarySearch + +Use recursion to perform a logarithmic search similar to `.indexOf()` that finds the index of a value within an array. The two differences being +1. Operation only works with sorted arrays +2. Offers a major performance boost when compared to a linear search or `.indexOf()` + +```js +const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { + if (start > end) return -1; + const mid = Math.floor((start + end) / 2); + const target = arr[mid]; + return (target > val) + ? binarySearch(arr, val, start, mid - 1) + : (target < val) + ? binarySearch(arr, val, mid + 1, end) + : mid +} +``` + +```js +binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2 +binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1 +```