Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Dec 19, 2023
1 parent ed51534 commit 8b6c553
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions coding_interviews/algoexpert/binary-search/binary-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function binarySearch(array, target) {
let start = 0;
let end = array.length - 1;
let middle = Math.floor((start + end) / 2);

while (start <= end) {
let num = array[middle];

if (num === target) {
return middle;
}

if (num > target) {
end = middle - 1;
}

if (num < target) {
start = middle + 1;
}

middle = Math.floor((start + end) / 2);
}

return -1;
}

0 comments on commit 8b6c553

Please sign in to comment.