diff --git a/Algorithms/binarySearch.js b/Algorithms/binarySearch.js new file mode 100644 index 0000000..b750a5e --- /dev/null +++ b/Algorithms/binarySearch.js @@ -0,0 +1,24 @@ +/** + * Binary search is an efficient algorithm for finding an item from a sorted list of items. + * It works by repeatedly dividing in half the portion of the list that could contain the item, + * until you've narrowed down the possible locations to just one. + */ + +function binarySearch(arr, x, left, right) { + if (left > right) { + return false; + } + + const middle = Math.floor(left + (right - left) / 2); + if (arr[middle] === x) { + return true; + } else if (x < arr[middle]) { + return binarySearch(arr, x, left, middle - 1); + } else { + return binarySearch(arr, x, middle + 1, right); + } +} + +export default function (arr, x) { + return binarySearch(arr, x, 0, arr.length); +}