forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
31 lines (27 loc) · 863 Bytes
/
binarySearch.js
File metadata and controls
31 lines (27 loc) · 863 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
31
/**
* Search a value into a sorted array by repeatedly dividing the search interval in half.
* @param {Array} arr Array to search into
* @param {Number} k Value to search
* @return {Number} index of found item or -1 for not found
*/
function binarySearch(arr, k) {
let min = 0
let max = arr.length - 1
while (min <= max) {
const cur = Math.floor((min + max) / 2)
if (arr[cur] === k) { return cur }
(arr[cur] > k)
? max = cur - 1
: min = cur + 1
}
return -1
}
const arr = [1,2,3,4,5,6,7,8,9,10]
console.log(binarySearch(arr, 6)) // 5
console.log(binarySearch(arr, 9)) // 8
console.log(binarySearch(arr, 2)) // 1
console.log(binarySearch(arr, 7)) // 6
console.log(binarySearch(arr, 11)) // -1
console.log(binarySearch(arr, 3)) // 2
console.log(binarySearch(arr, 3)) // 2
console.log(binarySearch(arr, 3)) // 2