-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.js
70 lines (65 loc) · 1.68 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function () {
/**
*
* @param {Array} arr a sorted array of numbers
* @param {Number} searchNr the number that is present in the array
* @param {Number} startInd the starting index where we need to search
* @param {Number} endInd the ending index where we need to search
*
* @returns {Number} the index where searchNr is found
*/
function binarySearch(arr, searchNr, startInd = 0, endInd = null) {
// initialize endIndex
let endIndex = endInd;
if (!endIndex) endIndex = arr.length - 1;
// get the middle search index
const searchIndex = Math.floor((startInd + endIndex) / 2);
const searchedVal = arr[searchIndex];
if (startInd === endIndex && searchedVal !== searchNr) {
return false;
}
// Check the value of that key
// If searchedVal equals the searchNr return the index
if (searchedVal === searchNr) {
return searchIndex;
}
// if searchedVal is lower then call binarySearch with a new array with all index's above the middle index
else if (searchNr < searchedVal) {
return binarySearch(arr, searchNr, startInd, searchIndex - 1);
}
// if searchedVal is higher then call binarySearch with a new array with all index's below the middle index
else if (searchNr > searchedVal) {
return binarySearch(arr, searchNr, searchIndex + 1, endIndex);
}
}
const primes = [
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
];
console.log(binarySearch(primes, 3));
console.log(binarySearch(primes, 53));
console.log(binarySearch(primes, 8));
})();