Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 31344fa

Browse files
committedJul 18, 2018
Add tests for Jump Search.
1 parent a327b68 commit 31344fa

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ a set of rules that precisely define a sequence of operations.
8484
* `A` [Regular Expression Matching](src/algorithms/string/regular-expression-matching)
8585
* **Searches**
8686
* `B` [Linear Search](src/algorithms/search/linear-search)
87-
* `B` [Jump Search](src/algorithms/search/jump-search)
88-
* `B` [Binary Search](src/algorithms/search/binary-search)
87+
* `B` [Jump Search](src/algorithms/search/jump-search) (or Block Search) - search in sorted array
88+
* `B` [Binary Search](src/algorithms/search/binary-search) - search in sorted array
8989
* **Sorting**
9090
* `B` [Bubble Sort](src/algorithms/sorting/bubble-sort)
9191
* `B` [Selection Sort](src/algorithms/sorting/selection-sort)

‎src/algorithms/search/jump-search/__test__/jumpSearch.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@ describe('jumpSearch', () => {
1717
expect(jumpSearch([1, 2, 5, 10, 20, 21, 24, 30, 48], 30)).toBe(7);
1818
expect(jumpSearch([1, 2, 5, 10, 20, 21, 24, 30, 48], 48)).toBe(8);
1919
});
20+
21+
it('should search object in sorted array', () => {
22+
const sortedArrayOfObjects = [
23+
{ key: 1, value: 'value1' },
24+
{ key: 2, value: 'value2' },
25+
{ key: 3, value: 'value3' },
26+
];
27+
28+
const comparator = (a, b) => {
29+
if (a.key === b.key) return 0;
30+
return a.key < b.key ? -1 : 1;
31+
};
32+
33+
expect(jumpSearch([], { key: 1 }, comparator)).toBe(-1);
34+
expect(jumpSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(-1);
35+
expect(jumpSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0);
36+
expect(jumpSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1);
37+
expect(jumpSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2);
38+
});
2039
});

0 commit comments

Comments
 (0)
Please sign in to comment.