From 332bebc151a8b986f2fabb160e838e6d02a2d9d0 Mon Sep 17 00:00:00 2001 From: Gabriel Barker Date: Fri, 11 Oct 2019 16:46:00 +0100 Subject: [PATCH 1/3] Add jumpSearch algorithm --- src/_Searching_/JumpSearch/index.js | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/_Searching_/JumpSearch/index.js diff --git a/src/_Searching_/JumpSearch/index.js b/src/_Searching_/JumpSearch/index.js new file mode 100644 index 00000000..dd0761aa --- /dev/null +++ b/src/_Searching_/JumpSearch/index.js @@ -0,0 +1,31 @@ +function jumpSearch(arr, jump, key) { + // treat jumps larger than array size as linear search + jump = jump > arr.length ? 1 : jump; + + let low = 0, high = 0; + for (let i = jump; i < arr.length; i+=jump) { + if(key < arr[i]) { + high = i; + low = i - jump; + break; + } + } + + // check if loop has finished without finding key + if(high == 0) { + high = arr.length; + low = high - jump; + } + + for(let i = low; i < high; i++) { + if(arr[i] == key) { + // return the index of the key + return i + 1; + } + } + return null; + } + + module.exports = { + jumpSearch, + }; \ No newline at end of file From 9f8fabeb371e660401549f33cc1bffb31b235e1a Mon Sep 17 00:00:00 2001 From: Gabriel Barker Date: Fri, 11 Oct 2019 17:12:16 +0100 Subject: [PATCH 2/3] Test jumpSeach method --- src/_Searching_/JumpSearch/JumpSearch.test.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/_Searching_/JumpSearch/JumpSearch.test.js diff --git a/src/_Searching_/JumpSearch/JumpSearch.test.js b/src/_Searching_/JumpSearch/JumpSearch.test.js new file mode 100644 index 00000000..18988dc4 --- /dev/null +++ b/src/_Searching_/JumpSearch/JumpSearch.test.js @@ -0,0 +1,26 @@ +const { jumpSearch, jumpSearchRecursive } = require('.'); + +describe('Jump Search', () => { +    const array = [1, 2, 3, 4, 5, 6, 7, 8]; +    const jump = 3; +    describe('When element to find is at 1st position ', () => { +        it('Jump search', () => { +          expect(jumpSearch(array, jump, 1)).toEqual(0); +        }); +      }); +    describe('When element to find is at last position ', () => { +        it('Jump search', () => { +          expect(jumpSearch(array, jump, 7)).toEqual(6); +        }); +      }); +    describe('When element to find is at random position ', () => { +        it('Jump search', () => { +          expect(jumpSearch(array, jump, 3)).toEqual(2); +        }); +      }); +    describe('When jump is larger than array', () => { +        it('Jump search', () => { +          expect(jumpSearch(array, 9, 3)).toEqual(2); +        }); +      }); +}); \ No newline at end of file From ce484cd416890800e0f5867f566be98b3ca75c29 Mon Sep 17 00:00:00 2001 From: Gabriel Barker Date: Fri, 11 Oct 2019 17:16:14 +0100 Subject: [PATCH 3/3] fix minor error --- src/_Searching_/JumpSearch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_Searching_/JumpSearch/index.js b/src/_Searching_/JumpSearch/index.js index dd0761aa..cf2699ce 100644 --- a/src/_Searching_/JumpSearch/index.js +++ b/src/_Searching_/JumpSearch/index.js @@ -20,7 +20,7 @@ function jumpSearch(arr, jump, key) { for(let i = low; i < high; i++) { if(arr[i] == key) { // return the index of the key - return i + 1; + return i; } } return null;