forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
30 lines (24 loc) · 711 Bytes
/
binarySearch.js
File metadata and controls
30 lines (24 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function binarySearch(arr, x) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.round((left + right) / 2); // calculate the mid index
// if value is same as value on midIndex, simply return midIndex
if (arr[mid] == x) {
return mid;
}
// when value is less than middle value, we search in the left half
if (arr[mid] < x) {
left = mid + 1;
// when value is greater than middle value, we search in the right half
} else {
right = mid - 1;
}
}
return -1;
}
// testCase
// sorted array to search the value in
const arr = [2, 12, 23, 34, 45, 52, 61, 75, 85, 99];
const x = 85;
console.log(binarySearch(arr, x));