mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add tests for Jump Search.
This commit is contained in:
parent
a327b68a06
commit
31344fa6a2
@ -84,8 +84,8 @@ a set of rules that precisely define a sequence of operations.
|
||||
* `A` [Regular Expression Matching](src/algorithms/string/regular-expression-matching)
|
||||
* **Searches**
|
||||
* `B` [Linear Search](src/algorithms/search/linear-search)
|
||||
* `B` [Jump Search](src/algorithms/search/jump-search)
|
||||
* `B` [Binary Search](src/algorithms/search/binary-search)
|
||||
* `B` [Jump Search](src/algorithms/search/jump-search) (or Block Search) - search in sorted array
|
||||
* `B` [Binary Search](src/algorithms/search/binary-search) - search in sorted array
|
||||
* **Sorting**
|
||||
* `B` [Bubble Sort](src/algorithms/sorting/bubble-sort)
|
||||
* `B` [Selection Sort](src/algorithms/sorting/selection-sort)
|
||||
|
@ -17,4 +17,23 @@ describe('jumpSearch', () => {
|
||||
expect(jumpSearch([1, 2, 5, 10, 20, 21, 24, 30, 48], 30)).toBe(7);
|
||||
expect(jumpSearch([1, 2, 5, 10, 20, 21, 24, 30, 48], 48)).toBe(8);
|
||||
});
|
||||
|
||||
it('should search object in sorted array', () => {
|
||||
const sortedArrayOfObjects = [
|
||||
{ key: 1, value: 'value1' },
|
||||
{ key: 2, value: 'value2' },
|
||||
{ key: 3, value: 'value3' },
|
||||
];
|
||||
|
||||
const comparator = (a, b) => {
|
||||
if (a.key === b.key) return 0;
|
||||
return a.key < b.key ? -1 : 1;
|
||||
};
|
||||
|
||||
expect(jumpSearch([], { key: 1 }, comparator)).toBe(-1);
|
||||
expect(jumpSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(-1);
|
||||
expect(jumpSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0);
|
||||
expect(jumpSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1);
|
||||
expect(jumpSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user