Add tests for Jump Search.

This commit is contained in:
Oleksii Trekhleb 2018-07-18 12:03:21 +03:00
parent a327b68a06
commit 31344fa6a2
2 changed files with 21 additions and 2 deletions

View File

@ -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)

View File

@ -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);
});
});