From 31344fa6a2f0d621e7b957bf6375c1b061fe5c90 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Wed, 18 Jul 2018 12:03:21 +0300 Subject: [PATCH] Add tests for Jump Search. --- README.md | 4 ++-- .../jump-search/__test__/jumpSearch.test.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4438e694..5302c78f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/algorithms/search/jump-search/__test__/jumpSearch.test.js b/src/algorithms/search/jump-search/__test__/jumpSearch.test.js index 78eb3a67..32f4d3f7 100644 --- a/src/algorithms/search/jump-search/__test__/jumpSearch.test.js +++ b/src/algorithms/search/jump-search/__test__/jumpSearch.test.js @@ -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); + }); });