Skip to content

Commit f30239a

Browse files
authored
Merge pull request vJechsmayr#64 from josehkburger/binarySearch
Binary search
2 parents 8fbc2a8 + 22d040d commit f30239a

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎SearchAlgorithms/binarySearch.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function binarySearch(arr, target) {
2+
let left = 0;
3+
let right = arr.length - 1;
4+
while (left <= right) {
5+
const mid = left + Math.floor((right - left) / 2);
6+
if (arr[mid] === target) {
7+
return mid;
8+
}
9+
if (arr[mid] < target) {
10+
left = mid + 1;
11+
} else {
12+
right = mid - 1;
13+
}
14+
}
15+
return -1;
16+
}

0 commit comments

Comments
 (0)