Skip to content

Commit c7a13e7

Browse files
authored
Add Interpolation search (#199)
* Add Interpolation search * Add new algorithm to directory --------- Co-authored-by: IcarusTheFly <[email protected]>
1 parent c9b3d38 commit c7a13e7

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139

140140
## Search
141141
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts)
142+
* [Interpolation Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/interpolation_search.ts)
142143
* [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts)
143144
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)
144145
* [Sentinel Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/sentinel_search.ts)

search/interpolation_search.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @function interpolationSearch
3+
* @description Interpolation search is an algorithm for searching for a
4+
* key in an array that has been ordered by numerical values assigned
5+
* to the keys (key values)
6+
* @param {number[]} array - list of numbers
7+
* @param {number} target - target number to search for
8+
* @return {number} - index of the target number in the list, or -1 if not found
9+
* @see https://en.wikipedia.org/wiki/Interpolation_search
10+
* @example interpolationSearch([1, 3, 5, 7, 9, 11], 1) => 0
11+
*/
12+
export const interpolationSearch = (array: number[], target: number): number => {
13+
let lowIndex: number = 0;
14+
let highIndex: number = array.length - 1;
15+
let currentValue: number = array[lowIndex];
16+
let pos: number = 0;
17+
18+
while (lowIndex <= highIndex) {
19+
const lowValue: number = array[lowIndex];
20+
const highValue: number = array[highIndex];
21+
22+
if (lowValue === highValue) {
23+
if (array[lowIndex] === target) {
24+
return lowIndex;
25+
}
26+
break;
27+
}
28+
29+
pos = Math.round(lowIndex + (target-lowValue)*(highIndex-lowIndex) / (highValue-lowValue));
30+
31+
if (pos < 0 || pos >= array.length) {
32+
break;
33+
}
34+
35+
currentValue = array[pos];
36+
37+
if (target === currentValue) {
38+
return pos;
39+
}
40+
41+
if (target > currentValue) {
42+
lowIndex = pos + 1;
43+
} else {
44+
highIndex = pos - 1;
45+
}
46+
}
47+
48+
return -1;
49+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { interpolationSearch } from "../interpolation_search";
2+
3+
describe("Interpolation search", () => {
4+
test.each([
5+
[[1, 3, 5, 7, 9, 11], 1, 0],
6+
[[1, 3, 7, 10, 14, 15, 16, 18, 20, 21, 22, 23, 25, 33, 35, 42, 45, 47, 50, 52], 33, 13],
7+
[[0, 45, 67, 70, 89, 129, 150, 308], 308, 7],
8+
[[0, 45, 67, 70, 89, 129, 150, 308], 190, -1]
9+
])(
10+
"of %o, searching for %o, expected %i",
11+
(array: any[], target: any, index: number) => {
12+
expect(interpolationSearch(array, target)).toStrictEqual(index)
13+
},
14+
);
15+
});

0 commit comments

Comments
 (0)