binarySearch

说明

简单实现二分查找算法

源码

用法

binarySearch.test.ts
1
import binarySearch from "../../utility/others/binarySearch";
2
3
describe('binarySearch tests...', () => {
4
test('The method named `binarySearch() should return a correct index value', () => {
5
const arr = [10, 867, 4, 34, 9, 98, 45, 67];
6
const received = [
7
{
8
arr,
9
value: 34,
10
},
11
{
12
arr,
13
value: 99,
14
},
15
];
16
const expected = [3, -1];
17
18
for (const [key, value] of received.entries()) {
19
const result = binarySearch(value.arr, value.value);
20
21
expect(result).toBe(expected[key]);
22
}
23
});
24
});
Copied!
最近更新 11mo ago
复制链接