-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
36 lines (30 loc) · 878 Bytes
/
index.ts
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
function interpolationSearch<T>(haystack: number[], needle: number) {
let low = 0;
let middle = -1;
let high = haystack.length - 1;
const match =
haystack[high] !== haystack[low] &&
needle >= haystack[low] &&
needle <= haystack[high];
while (match) {
middle =
low +
((high - low) / (haystack[high] - haystack[low])) *
(needle - haystack[low]);
console.log({ haystack, middle, needle });
if (haystack[middle] === needle) {
console.log(`Found needle in haystack #${middle}`);
return haystack[middle];
} else if (haystack[middle] < needle) {
low = middle + 1;
} else if (haystack[middle] > needle) {
high = middle - 1;
}
}
if (needle === haystack[low]) {
return low;
}
}
const array = [...Array(100).keys()];
// delete array[99];
console.log(interpolationSearch(array, 99));