-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
33 lines (31 loc) · 806 Bytes
/
binarySearch.js
File metadata and controls
33 lines (31 loc) · 806 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
32
33
/**
* Running time = O(log n)
* Ω(1) in best case
* Binary Search in a array of numbers ordered
* @param {number} search number in array
* @param {Array} array of numbers ordered
* @returns {number} index in array where number was found
*/
function binarySearch(search, arr) {
let low = 0
let high = arr.length - 1
while (low <= high) {
let middle = Math.floor((low + high) / 2)
let guess = arr[middle]
if (guess === search) {
return middle
} else if (guess < search) {
low = middle + 1
} else {
high = middle - 1
}
}
return -1
}
const arr = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const index = binarySearch(13, arr);
if (index != -1) {
console.log(`Number found at index %d`, index)
} else {
console.log(`Number not found at index`)
}