forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Add exponential search #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GohJunLe
wants to merge
23
commits into
master
Choose a base branch
from
change
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e3d9057
Add exponential search
GohJunLe 6197291
Merge pull request #1 from GohJunLe/GohJunLe-patch-1
GohJunLe c8fca7d
create README for exponential search
GohJunLe 41038d3
Update data-structure images.
trekhleb 8c7950a
Update data-structure images.
trekhleb a81c0bd
Update data-structure images.
trekhleb 6914c65
Update data-structure images.
trekhleb f9b558b
Update data-structure images.
trekhleb 1d02cd6
Update data-structure images.
trekhleb 4d2d718
Add a link to minimalistic data structure sketches. (#933)
trekhleb 0203bfe
Merge branch 'trekhleb:master' into change
GohJunLe 2913a98
Delete src/algorithms/search/exponential-search directory
GohJunLe 1baf610
Add exponential search
GohJunLe 295fb99
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe 8e37e8f
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe dd1843f
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe 7c13298
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe fbf12c3
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe 820492a
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe f1d7c4b
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe 01ca43d
Update exponentialSearch.js
GohJunLe 38d1819
Update src/algorithms/search/exponential-search/exponentialSearch.js
GohJunLe c277288
Update README.md
GohJunLe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
|
||
|  | ||
|
|
||
| ## 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) |
35 changes: 35 additions & 0 deletions
35
src/algorithms/search/exponential-search/__test__/exponentialSearch.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }); | ||
| }); |
56 changes: 56 additions & 0 deletions
56
src/algorithms/search/exponential-search/exponentialSearch.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 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 *= 2; | ||
| } | ||
| // Call binary search for the found range. | ||
| return binarySearch(sortedArray, range/2, Math.min(range, length - 1), seekElement, comparatorCallback); | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+46.1 KB
(130%)
src/data-structures/tree/binary-search-tree/images/binary-search-tree.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.