From cd81f3b0fd97c55ced2c1242021cdcf860a995e1 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Mon, 8 Aug 2022 19:25:02 +0800 Subject: [PATCH 01/16] Add files via upload --- .../__test__/exponentialSearch.test.js | 35 ++++++++++ .../exponential-search/exponentialSearch.js | 67 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js create mode 100644 src/algorithms/search/exponential-search/exponentialSearch.js diff --git a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js new file mode 100644 index 00000000..0816af2b --- /dev/null +++ b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js @@ -0,0 +1,35 @@ +import exponentialSearch from '../exponentialSearch'; + +describe('exponentialSearch', () => { + it('should search number in sorted array', () => { + expect(exponentialSearch([], 1)).toBe(-1); + expect(exponentialSearch([1], 1)).toBe(0); + expect(exponentialSearch([1, 2], 1)).toBe(0); + expect(exponentialSearch([1, 2], 2)).toBe(1); + expect(exponentialSearch([1, 5, 10, 12], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1); + }); + + it('should search object in sorted array', () => { + const sortedArrayOfObjects = [ + { key: 1, value: 'value1' }, + { key: 2, value: 'value2' }, + { key: 3, value: 'value3' }, + { key: 4, value: 'value4' }, + ]; + + const comparator = (a, b) => { + if (a.key === b.key) return 0; + return a.key < b.key ? -1 : 1; + }; + + expect(exponentialSearch([], { key: 1 }, comparator)).toBe(-1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(3); + expect(exponentialSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0); + expect(exponentialSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2); + }); +}); diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js new file mode 100644 index 00000000..b9c69693 --- /dev/null +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -0,0 +1,67 @@ +import Comparator from '../../../utils/comparator/Comparator'; + +/** + * Binary search implementation. + * + * @param {*[]} sortedArray + * @param {*} startIndex + * @param {*} endIndex + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ + +function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparatorCallback) +{ + const comparator = new Comparator(comparatorCallback); + + if (endIndex >= startIndex) + { + const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); + + // If the element is present at the middle itself + if (comparator.equal(sortedArray[middleIndex], seekElement)) + return middleIndex; + + // If element is smaller than middleIndex, then it can only be present n left subarray + if (comparator.greaterThan(sortedArray[middleIndex], seekElement)){ + return binarySearch(sortedArray, startIndex, middleIndex - 1, seekElement, comparatorCallback); + }else{ + // Else the element can only be present in right subarray + return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); + } + + } + + // We reach here when element is not present in array + return -1; +} + +/** + * Exponential search implementation. + * + * @param {*[]} sortedArray + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ + +export default function exponentialSearch(sortedArray, seekElement, comparatorCallback) +{ + const comparator = new Comparator(comparatorCallback); + const length = sortedArray.length; + + // If element is present at first location itself + if(sortedArray.length != 0){ + if (comparator.equal(sortedArray[0], seekElement)) + return 0; + } + + // Find range for binary search by repeated doubling + let range = 1; + while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) + range = range * 2; + + // Call binary search for the found range. + return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); +} \ No newline at end of file From 8ee67a4706957319aa0399c9c8bbdb03f985255c Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Mon, 8 Aug 2022 19:38:32 +0800 Subject: [PATCH 02/16] Delete src/algorithms/search/exponential-search directory --- .../__test__/exponentialSearch.test.js | 35 ---------- .../exponential-search/exponentialSearch.js | 67 ------------------- 2 files changed, 102 deletions(-) delete mode 100644 src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js delete mode 100644 src/algorithms/search/exponential-search/exponentialSearch.js diff --git a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js deleted file mode 100644 index 0816af2b..00000000 --- a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js +++ /dev/null @@ -1,35 +0,0 @@ -import exponentialSearch from '../exponentialSearch'; - -describe('exponentialSearch', () => { - it('should search number in sorted array', () => { - expect(exponentialSearch([], 1)).toBe(-1); - expect(exponentialSearch([1], 1)).toBe(0); - expect(exponentialSearch([1, 2], 1)).toBe(0); - expect(exponentialSearch([1, 2], 2)).toBe(1); - expect(exponentialSearch([1, 5, 10, 12], 1)).toBe(0); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1); - }); - - it('should search object in sorted array', () => { - const sortedArrayOfObjects = [ - { key: 1, value: 'value1' }, - { key: 2, value: 'value2' }, - { key: 3, value: 'value3' }, - { key: 4, value: 'value4' }, - ]; - - const comparator = (a, b) => { - if (a.key === b.key) return 0; - return a.key < b.key ? -1 : 1; - }; - - expect(exponentialSearch([], { key: 1 }, comparator)).toBe(-1); - expect(exponentialSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(3); - expect(exponentialSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0); - expect(exponentialSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1); - expect(exponentialSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2); - }); -}); diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js deleted file mode 100644 index b9c69693..00000000 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ /dev/null @@ -1,67 +0,0 @@ -import Comparator from '../../../utils/comparator/Comparator'; - -/** - * Binary search implementation. - * - * @param {*[]} sortedArray - * @param {*} startIndex - * @param {*} endIndex - * @param {*} seekElement - * @param {function(a, b)} [comparatorCallback] - * @return {number} - */ - -function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparatorCallback) -{ - const comparator = new Comparator(comparatorCallback); - - if (endIndex >= startIndex) - { - const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); - - // If the element is present at the middle itself - if (comparator.equal(sortedArray[middleIndex], seekElement)) - return middleIndex; - - // If element is smaller than middleIndex, then it can only be present n left subarray - if (comparator.greaterThan(sortedArray[middleIndex], seekElement)){ - return binarySearch(sortedArray, startIndex, middleIndex - 1, seekElement, comparatorCallback); - }else{ - // Else the element can only be present in right subarray - return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); - } - - } - - // We reach here when element is not present in array - return -1; -} - -/** - * Exponential search implementation. - * - * @param {*[]} sortedArray - * @param {*} seekElement - * @param {function(a, b)} [comparatorCallback] - * @return {number} - */ - -export default function exponentialSearch(sortedArray, seekElement, comparatorCallback) -{ - const comparator = new Comparator(comparatorCallback); - const length = sortedArray.length; - - // If element is present at first location itself - if(sortedArray.length != 0){ - if (comparator.equal(sortedArray[0], seekElement)) - return 0; - } - - // Find range for binary search by repeated doubling - let range = 1; - while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) - range = range * 2; - - // Call binary search for the found range. - return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); -} \ No newline at end of file From e3d90576a92e372dbcbb6109571774818bfb0b4c Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Tue, 9 Aug 2022 08:36:48 +0800 Subject: [PATCH 03/16] Add exponential search --- .../__test__/exponentialSearch.test.js | 35 ++++++++++ .../exponential-search/exponentialSearch.js | 67 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js create mode 100644 src/algorithms/search/exponential-search/exponentialSearch.js diff --git a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js new file mode 100644 index 00000000..0816af2b --- /dev/null +++ b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js @@ -0,0 +1,35 @@ +import exponentialSearch from '../exponentialSearch'; + +describe('exponentialSearch', () => { + it('should search number in sorted array', () => { + expect(exponentialSearch([], 1)).toBe(-1); + expect(exponentialSearch([1], 1)).toBe(0); + expect(exponentialSearch([1, 2], 1)).toBe(0); + expect(exponentialSearch([1, 2], 2)).toBe(1); + expect(exponentialSearch([1, 5, 10, 12], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1); + }); + + it('should search object in sorted array', () => { + const sortedArrayOfObjects = [ + { key: 1, value: 'value1' }, + { key: 2, value: 'value2' }, + { key: 3, value: 'value3' }, + { key: 4, value: 'value4' }, + ]; + + const comparator = (a, b) => { + if (a.key === b.key) return 0; + return a.key < b.key ? -1 : 1; + }; + + expect(exponentialSearch([], { key: 1 }, comparator)).toBe(-1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(3); + expect(exponentialSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0); + expect(exponentialSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2); + }); +}); diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js new file mode 100644 index 00000000..b9c69693 --- /dev/null +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -0,0 +1,67 @@ +import Comparator from '../../../utils/comparator/Comparator'; + +/** + * Binary search implementation. + * + * @param {*[]} sortedArray + * @param {*} startIndex + * @param {*} endIndex + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ + +function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparatorCallback) +{ + const comparator = new Comparator(comparatorCallback); + + if (endIndex >= startIndex) + { + const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); + + // If the element is present at the middle itself + if (comparator.equal(sortedArray[middleIndex], seekElement)) + return middleIndex; + + // If element is smaller than middleIndex, then it can only be present n left subarray + if (comparator.greaterThan(sortedArray[middleIndex], seekElement)){ + return binarySearch(sortedArray, startIndex, middleIndex - 1, seekElement, comparatorCallback); + }else{ + // Else the element can only be present in right subarray + return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); + } + + } + + // We reach here when element is not present in array + return -1; +} + +/** + * Exponential search implementation. + * + * @param {*[]} sortedArray + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ + +export default function exponentialSearch(sortedArray, seekElement, comparatorCallback) +{ + const comparator = new Comparator(comparatorCallback); + const length = sortedArray.length; + + // If element is present at first location itself + if(sortedArray.length != 0){ + if (comparator.equal(sortedArray[0], seekElement)) + return 0; + } + + // Find range for binary search by repeated doubling + let range = 1; + while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) + range = range * 2; + + // Call binary search for the found range. + return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); +} \ No newline at end of file From c8fca7d2a8054dd856b701303113c38213818191 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Tue, 9 Aug 2022 08:48:47 +0800 Subject: [PATCH 04/16] create README for exponential search --- .../search/exponential-search/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/algorithms/search/exponential-search/README.md diff --git a/src/algorithms/search/exponential-search/README.md b/src/algorithms/search/exponential-search/README.md new file mode 100644 index 00000000..77f7cce6 --- /dev/null +++ b/src/algorithms/search/exponential-search/README.md @@ -0,0 +1,16 @@ +# Exponential Search + +In computer science, an exponential search is an algorithm.There are numerous ways to implement this with the most common being to determine a range that the search key +resides in and performing a binary search within that range. This takes O(log i) where i is the position of the search key in the list, if the search key is in the list, +or the position where the search key should be, if the search key is not in the list.Exponential search can also be used to search in bounded lists. Exponential search +can even out-perform more traditional searches for bounded lists, such as binary search, when the element being searched for is near the beginning of the array. + +![Exponential Search](https://upload.wikimedia.org/wikipedia/commons/4/45/Exponential_search.svg) + +## Complexity + +**Time Complexity**: `O(log i)` - i is the index of the element being searched for in the list + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Exponential_search) From 2913a98023bfa630e2e363578cff881193f4c23e Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 12:37:55 +0800 Subject: [PATCH 05/16] Delete src/algorithms/search/exponential-search directory --- .../search/exponential-search/README.md | 16 ----- .../__test__/exponentialSearch.test.js | 35 ---------- .../exponential-search/exponentialSearch.js | 67 ------------------- 3 files changed, 118 deletions(-) delete mode 100644 src/algorithms/search/exponential-search/README.md delete mode 100644 src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js delete mode 100644 src/algorithms/search/exponential-search/exponentialSearch.js diff --git a/src/algorithms/search/exponential-search/README.md b/src/algorithms/search/exponential-search/README.md deleted file mode 100644 index 77f7cce6..00000000 --- a/src/algorithms/search/exponential-search/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Exponential Search - -In computer science, an exponential search is an algorithm.There are numerous ways to implement this with the most common being to determine a range that the search key -resides in and performing a binary search within that range. This takes O(log i) where i is the position of the search key in the list, if the search key is in the list, -or the position where the search key should be, if the search key is not in the list.Exponential search can also be used to search in bounded lists. Exponential search -can even out-perform more traditional searches for bounded lists, such as binary search, when the element being searched for is near the beginning of the array. - -![Exponential Search](https://upload.wikimedia.org/wikipedia/commons/4/45/Exponential_search.svg) - -## Complexity - -**Time Complexity**: `O(log i)` - i is the index of the element being searched for in the list - -## References - -- [Wikipedia](https://en.wikipedia.org/wiki/Exponential_search) diff --git a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js deleted file mode 100644 index 0816af2b..00000000 --- a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js +++ /dev/null @@ -1,35 +0,0 @@ -import exponentialSearch from '../exponentialSearch'; - -describe('exponentialSearch', () => { - it('should search number in sorted array', () => { - expect(exponentialSearch([], 1)).toBe(-1); - expect(exponentialSearch([1], 1)).toBe(0); - expect(exponentialSearch([1, 2], 1)).toBe(0); - expect(exponentialSearch([1, 2], 2)).toBe(1); - expect(exponentialSearch([1, 5, 10, 12], 1)).toBe(0); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7); - expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1); - }); - - it('should search object in sorted array', () => { - const sortedArrayOfObjects = [ - { key: 1, value: 'value1' }, - { key: 2, value: 'value2' }, - { key: 3, value: 'value3' }, - { key: 4, value: 'value4' }, - ]; - - const comparator = (a, b) => { - if (a.key === b.key) return 0; - return a.key < b.key ? -1 : 1; - }; - - expect(exponentialSearch([], { key: 1 }, comparator)).toBe(-1); - expect(exponentialSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(3); - expect(exponentialSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0); - expect(exponentialSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1); - expect(exponentialSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2); - }); -}); diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js deleted file mode 100644 index b9c69693..00000000 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ /dev/null @@ -1,67 +0,0 @@ -import Comparator from '../../../utils/comparator/Comparator'; - -/** - * Binary search implementation. - * - * @param {*[]} sortedArray - * @param {*} startIndex - * @param {*} endIndex - * @param {*} seekElement - * @param {function(a, b)} [comparatorCallback] - * @return {number} - */ - -function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparatorCallback) -{ - const comparator = new Comparator(comparatorCallback); - - if (endIndex >= startIndex) - { - const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); - - // If the element is present at the middle itself - if (comparator.equal(sortedArray[middleIndex], seekElement)) - return middleIndex; - - // If element is smaller than middleIndex, then it can only be present n left subarray - if (comparator.greaterThan(sortedArray[middleIndex], seekElement)){ - return binarySearch(sortedArray, startIndex, middleIndex - 1, seekElement, comparatorCallback); - }else{ - // Else the element can only be present in right subarray - return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); - } - - } - - // We reach here when element is not present in array - return -1; -} - -/** - * Exponential search implementation. - * - * @param {*[]} sortedArray - * @param {*} seekElement - * @param {function(a, b)} [comparatorCallback] - * @return {number} - */ - -export default function exponentialSearch(sortedArray, seekElement, comparatorCallback) -{ - const comparator = new Comparator(comparatorCallback); - const length = sortedArray.length; - - // If element is present at first location itself - if(sortedArray.length != 0){ - if (comparator.equal(sortedArray[0], seekElement)) - return 0; - } - - // Find range for binary search by repeated doubling - let range = 1; - while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) - range = range * 2; - - // Call binary search for the found range. - return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); -} \ No newline at end of file From 1baf6106fd7f435a76f6bf794153e9035028d73c Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 12:38:31 +0800 Subject: [PATCH 06/16] Add exponential search --- .../search/exponential-search/README.md | 16 ++++++ .../__test__/exponentialSearch.test.js | 35 ++++++++++++ .../exponential-search/exponentialSearch.js | 55 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/algorithms/search/exponential-search/README.md create mode 100644 src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js create mode 100644 src/algorithms/search/exponential-search/exponentialSearch.js diff --git a/src/algorithms/search/exponential-search/README.md b/src/algorithms/search/exponential-search/README.md new file mode 100644 index 00000000..77f7cce6 --- /dev/null +++ b/src/algorithms/search/exponential-search/README.md @@ -0,0 +1,16 @@ +# Exponential Search + +In computer science, an exponential search is an algorithm.There are numerous ways to implement this with the most common being to determine a range that the search key +resides in and performing a binary search within that range. This takes O(log i) where i is the position of the search key in the list, if the search key is in the list, +or the position where the search key should be, if the search key is not in the list.Exponential search can also be used to search in bounded lists. Exponential search +can even out-perform more traditional searches for bounded lists, such as binary search, when the element being searched for is near the beginning of the array. + +![Exponential Search](https://upload.wikimedia.org/wikipedia/commons/4/45/Exponential_search.svg) + +## Complexity + +**Time Complexity**: `O(log i)` - i is the index of the element being searched for in the list + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Exponential_search) diff --git a/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js new file mode 100644 index 00000000..0816af2b --- /dev/null +++ b/src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js @@ -0,0 +1,35 @@ +import exponentialSearch from '../exponentialSearch'; + +describe('exponentialSearch', () => { + it('should search number in sorted array', () => { + expect(exponentialSearch([], 1)).toBe(-1); + expect(exponentialSearch([1], 1)).toBe(0); + expect(exponentialSearch([1, 2], 1)).toBe(0); + expect(exponentialSearch([1, 2], 2)).toBe(1); + expect(exponentialSearch([1, 5, 10, 12], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7); + expect(exponentialSearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1); + }); + + it('should search object in sorted array', () => { + const sortedArrayOfObjects = [ + { key: 1, value: 'value1' }, + { key: 2, value: 'value2' }, + { key: 3, value: 'value3' }, + { key: 4, value: 'value4' }, + ]; + + const comparator = (a, b) => { + if (a.key === b.key) return 0; + return a.key < b.key ? -1 : 1; + }; + + expect(exponentialSearch([], { key: 1 }, comparator)).toBe(-1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(3); + expect(exponentialSearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0); + expect(exponentialSearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1); + expect(exponentialSearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2); + }); +}); diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js new file mode 100644 index 00000000..2c80d099 --- /dev/null +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -0,0 +1,55 @@ +import Comparator from '../../../utils/comparator/Comparator'; + +/** + * Binary search implementation. + * + * @param {*[]} sortedArray + * @param {*} startIndex + * @param {*} endIndex + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ + +function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparatorCallback) { + const comparator = new Comparator(comparatorCallback); + if (endIndex >= startIndex) { + const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2); + // If the element is present at the middle itself + if (comparator.equal(sortedArray[middleIndex], seekElement)) { + return middleIndex; + } + // If element is smaller than middleIndex, then it can only be present n left subarray + if (comparator.greaterThan(sortedArray[middleIndex], seekElement)) { + return binarySearch(sortedArray, startIndex, middleIndex-1, seekElement, comparatorCallback); + } + // Else the element can only be present in right subarray + return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); + } + // We reach here when element is not present in array + return -1; +} +/** + * Exponential search implementation. + * + * @param {*[]} sortedArray + * @param {*} seekElement + * @param {function(a, b)} [comparatorCallback] + * @return {number} + */ +export default function exponentialSearch(sortedArray, seekElement, comparatorCallback) { + const comparator = new Comparator(comparatorCallback); + const length = sortedArray.length; + // If element is present at first location itself + if (sortedArray.length !== 0) { + if (comparator.equal(sortedArray[0], seekElement)) + return 0; + } + // Find range for binary search by repeated doubling + let range = 1; + while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) { + range = range * 2; + } + // Call binary search for the found range. + return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); +} \ No newline at end of file From 295fb99242e73fc569753e2b596c7f295b313d58 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:17:30 +0800 Subject: [PATCH 07/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index 2c80d099..7dec02c0 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -44,7 +44,7 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa if (sortedArray.length !== 0) { if (comparator.equal(sortedArray[0], seekElement)) return 0; - } + } // Find range for binary search by repeated doubling let range = 1; while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) { From 8e37e8ff7a0d14e743b71c3cc55cc11d88f6278c Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:20:12 +0800 Subject: [PATCH 08/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index 7dec02c0..9817b3b4 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -21,7 +21,7 @@ function binarySearch(sortedArray, startIndex, endIndex, seekElement, comparator } // If element is smaller than middleIndex, then it can only be present n left subarray if (comparator.greaterThan(sortedArray[middleIndex], seekElement)) { - return binarySearch(sortedArray, startIndex, middleIndex-1, seekElement, comparatorCallback); + return binarySearch(sortedArray, startIndex, middleIndex - 1, seekElement, comparatorCallback); } // Else the element can only be present in right subarray return binarySearch(sortedArray, middleIndex + 1, endIndex, seekElement, comparatorCallback); From dd1843f0aede3dfc4f5db2a8d7cd439625daa4ce Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:21:15 +0800 Subject: [PATCH 09/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index 9817b3b4..d2259514 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -48,7 +48,7 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa // Find range for binary search by repeated doubling let range = 1; while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) { - range = range * 2; + range *= 2; } // Call binary search for the found range. return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); From 7c132980ab4261f76fdcf18a62086842a6e686f5 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:21:27 +0800 Subject: [PATCH 10/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index d2259514..af78b2c2 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -51,5 +51,5 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa range *= 2; } // Call binary search for the found range. - return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); + return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); } \ No newline at end of file From fbf12c3259987eebd73b6014a2073c89ac5a064d Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:21:32 +0800 Subject: [PATCH 11/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index af78b2c2..7a0ff056 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -50,6 +50,6 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) { range *= 2; } - // Call binary search for the found range. + // Call binary search for the found range. return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); } \ No newline at end of file From 820492a8a8bd8332a9ab34649d9c405b68e56e5a Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:21:39 +0800 Subject: [PATCH 12/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index 7a0ff056..1249a238 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -45,7 +45,7 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa if (comparator.equal(sortedArray[0], seekElement)) return 0; } - // Find range for binary search by repeated doubling + // Find range for binary search by repeated doubling let range = 1; while (range < length && comparator.lessThanOrEqual(sortedArray[range], seekElement)) { range *= 2; From f1d7c4b9aa1fa02c94836a1f018d0c4cfa51bc1f Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:23:41 +0800 Subject: [PATCH 13/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index 1249a238..eef84a6e 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -43,7 +43,7 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa // If element is present at first location itself if (sortedArray.length !== 0) { if (comparator.equal(sortedArray[0], seekElement)) - return 0; + } } // Find range for binary search by repeated doubling let range = 1; From 01ca43d3fd74ca6d5ad2aa2e5c7e4a464e9ecec2 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:28:47 +0800 Subject: [PATCH 14/16] Update exponentialSearch.js --- .../search/exponential-search/exponentialSearch.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index eef84a6e..d6588156 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -42,7 +42,8 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa const length = sortedArray.length; // If element is present at first location itself if (sortedArray.length !== 0) { - if (comparator.equal(sortedArray[0], seekElement)) + if (comparator.equal(sortedArray[0], seekElement)){ + return 0; } } // Find range for binary search by repeated doubling @@ -52,4 +53,4 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa } // Call binary search for the found range. return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); -} \ No newline at end of file +} From 38d181972b57b66003a7fe3b5bd1867ab33988c4 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 17:31:05 +0800 Subject: [PATCH 15/16] Update src/algorithms/search/exponential-search/exponentialSearch.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/search/exponential-search/exponentialSearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/exponentialSearch.js b/src/algorithms/search/exponential-search/exponentialSearch.js index d6588156..7d6b3c56 100644 --- a/src/algorithms/search/exponential-search/exponentialSearch.js +++ b/src/algorithms/search/exponential-search/exponentialSearch.js @@ -44,7 +44,7 @@ export default function exponentialSearch(sortedArray, seekElement, comparatorCa if (sortedArray.length !== 0) { if (comparator.equal(sortedArray[0], seekElement)){ return 0; - } + } } // Find range for binary search by repeated doubling let range = 1; From c277288598a512c3c9a195c3b6d63e4342fe3676 Mon Sep 17 00:00:00 2001 From: GohJunLe <108907711+GohJunLe@users.noreply.github.com> Date: Sat, 3 Sep 2022 18:06:18 +0800 Subject: [PATCH 16/16] Update README.md --- src/algorithms/search/exponential-search/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/exponential-search/README.md b/src/algorithms/search/exponential-search/README.md index 77f7cce6..f0f7b7d0 100644 --- a/src/algorithms/search/exponential-search/README.md +++ b/src/algorithms/search/exponential-search/README.md @@ -13,4 +13,4 @@ can even out-perform more traditional searches for bounded lists, such as binary ## References -- [Wikipedia](https://en.wikipedia.org/wiki/Exponential_search) +-[Wikipedia](https://en.wikipedia.org/wiki/Exponential_search)