-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathMetaBinarySearch.js
More file actions
28 lines (23 loc) · 798 Bytes
/
MetaBinarySearch.js
File metadata and controls
28 lines (23 loc) · 798 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
/**
* Meta Binary Search
* https://www.geeksforgeeks.org/meta-binary-search-one-sided-binary-search/
*
* Builds the index bit by bit from the most-significant bit to the least-significant bit.
* Works on sorted arrays and returns the index of target, or -1 if not found.
*/
function metaBinarySearch(sortedArray, target) {
if (!Array.isArray(sortedArray) || sortedArray.length === 0) {
return -1
}
const n = sortedArray.length
const maxBit = Math.floor(Math.log2(Math.max(1, n - 1)))
let position = 0
for (let bit = maxBit; bit >= 0; bit--) {
const candidate = position + 2 ** bit
if (candidate < n && sortedArray[candidate] <= target) {
position = candidate
}
}
return sortedArray[position] === target ? position : -1
}
export { metaBinarySearch }