forked from nas5w/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.js
31 lines (28 loc) · 838 Bytes
/
binarySearch.js
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
/**
* Binary search - looks at the midpoint of the sorted list
* If midpoint = itemToFind, we're done
* If midpoint is lower, then we look at all of the greater numbers
* If modpoint is greater, then we look at all of the lower numbers
* Big O = (log n)
* @param {*} list to search from
* @param {*} itemToFind item we want to find
*/
function binarySearch(list, itemToFind) {
let low = 0;
let high = list.length;
let counter = 0;
while(low <= high) {
counter = counter + 1;
let mid = Math.floor((low + high) / 2);
let guess = list[mid];
if (guess === itemToFind) {
return true;
} else if (guess > itemToFind) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
module.exports = binarySearch;