-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary-search.js
More file actions
38 lines (29 loc) · 909 Bytes
/
binary-search.js
File metadata and controls
38 lines (29 loc) · 909 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
34
35
36
37
38
//Binary search should quickly search an array in O(log N) fashion
//
function binarySearch(array, val){
'use strict';
let minimumIndex = 0;
let maxIndex = array.length - 1;
let currentIndex;
(function search(){
currentIndex = Math.floor((minimumIndex + maxIndex) / 2);
console.log(`curently looking between ${array[minimumIndex]} and ${array[maxIndex]}`);
if (array[currentIndex] === val ){
console.log(`FOUND IT! ${val} == ${array[currentIndex]}`);
return;
}
if (val > array[currentIndex]){
minimumIndex = currentIndex + 1;
}
if (val < array[currentIndex]){
maxIndex = currentIndex -1;
}
search();
})();
}
let arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
binarySearch(arr, 5);
binarySearch(arr, 3);
binarySearch(arr, 59);
binarySearch(arr, 61);
binarySearch(arr, 7);