-
-
Notifications
You must be signed in to change notification settings - Fork 450
Exponential search #247
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
Merged
Merged
Exponential search #247
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4dba63
Merge pull request #1 from TheAlgorithms/master
Fechuli 1861a87
feat: add exponential search
Fechuli 5b37d3b
test: add test for exponential search
Fechuli 44b698d
fix : prettier fix
Fechuli 61b1d22
fix: added parameters to iterative binary search
Fechuli 6d55e80
fix: prettier fix #2
Fechuli 2c6a281
fix: modified the return on binary search and related tests
Fechuli 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,40 @@ | ||
import { binarySearchIterative } from './binary_search' | ||
|
||
/** | ||
* @description Exponential search algorithm for a sorted array. | ||
* | ||
* The algorithm searches for a specific value in a sorted array by first finding a range | ||
* where the value may be present and then performing a binary search within that range. | ||
* | ||
* Compared with binary search, exponential search can be more convenient and advantageous | ||
* in cases where the element to be searched is closer to the beginning of the array, | ||
* thus avoiding several comparisons that would make the search more verbose. | ||
* | ||
* @param {number[]} array - sorted list of numbers | ||
* @param {number} x - target number to search for | ||
* @return {number | null} - index of the target number in the list, or null if not found | ||
* @see [ExponentialSearch](https://www.geeksforgeeks.org/exponential-search/) | ||
* @example exponentialSearch([1, 2, 3, 4, 5], 3) => 2 | ||
* @example exponentialSearch([10, 20, 30, 40, 50], 35) => null | ||
*/ | ||
|
||
export const exponentialSearch = ( | ||
array: number[], | ||
x: number | ||
): number | null => { | ||
const arrayLength = array.length | ||
if (arrayLength === 0) return null | ||
|
||
if (array[0] === x) return 0 | ||
|
||
let i = 1 | ||
while (i < arrayLength && array[i] <= x) { | ||
i = i * 2 | ||
} | ||
|
||
const start = Math.floor(i / 2) | ||
const end = Math.min(i, arrayLength - 1) | ||
const result = binarySearchIterative(array, x, start, end) | ||
|
||
return result | ||
} |
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,19 @@ | ||
import { exponentialSearch } from '../exponential_search' | ||
|
||
describe('Exponential search', () => { | ||
test.each([ | ||
[[1, 2, 3, 4, 5], 3, 2], | ||
[[10, 20, 30, 40, 50], 35, null], | ||
[[10, 20, 30, 40, 50], 10, 0], | ||
[[10, 20, 30, 40, 50], 50, 4], | ||
[[10, 20, 30, 40, 50], 60, null], | ||
[[], 10, null], | ||
[[1, 2, 3, 4, 5], 1, 0], | ||
[[1, 2, 3, 4, 5], 5, 4] | ||
])( | ||
'of %o, searching for %o, expected %i', | ||
(array: number[], target: number, expected: number | null) => { | ||
expect(exponentialSearch(array, target)).toBe(expected) | ||
} | ||
) | ||
}) |
Oops, something went wrong.
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.