-
Notifications
You must be signed in to change notification settings - Fork 0
/
20--binary-search.js
55 lines (51 loc) · 1.83 KB
/
20--binary-search.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
"use strict";
/**
* Performs a binary search, with the given comparison function.
*
* @param sortedHayStack Array or Indexable, must be sorted.
* @param needle Element whose insertion position is desired.
* @param comparisonFunction: Function
* * @description Compares two Elements of the array
* * @param A An array element
* * @param B An array element
* * @returns a number of the same sign as A - B as if they were numbers.
* @returns The insertion position for the needle in the hayStack, which is to say, the index where it should lie,
* * shifting the following elements by one. If the needle is in the haystack, it's postion will be returned.
*/
let binarySearch = function(sortedHayStack, needle, comparisonFunction) {
// Binary find the insert position
let insertionPosition = null;
let low = 0;
let high = sortedHayStack.length - 1 + 1;
let half;
let comparison;
let infiniteLoop = 0;
while (insertionPosition === null) {
half = (low + high) >> 1;
comparison = comparisonFunction(needle, sortedHayStack[half]);
if (comparison > 0) {
low = half + 1;
} else if (comparison < 0) {
high = half - 1 + 1;
} else if (comparison == 0) {
insertionPosition = half;
} else {
error("Something strange #code-ref(706987)");
}
if (insertionPosition === null && low >= high) {
insertionPosition = high;
}
if (infiniteLoop++ > 10000) {
error("Infinite loop (>10000) #code-ref(102802)");
}
}
// Go to the lowest insert position
while (
insertionPosition >= 0
&& comparisonFunction(needle, sortedHayStack[insertionPosition]) <= 0
) {
insertionPosition -= 1;
}
insertionPosition += 1;
return insertionPosition;
};