Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Algorithms/binarySearch.js
Original file line number Diff line number Diff line change
@@ -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);
}