-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add meta binary search implementation #1894
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
nickzerjeski
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
nickzerjeski:feat-meta-binary-search-1835
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.
+58
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0de1aa1
feat: add meta binary search algorithm
nickzerjeski 3a48a80
Updated Documentation in README.md
nickzerjeski b2269fc
Address review edge cases in meta binary search
nickzerjeski 031541d
Update Search/test/MetaBinarySearch.test.js
nickzerjeski 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,28 @@ | ||
| /** | ||
| * Meta Binary Search | ||
| * https://www.geeksforgeeks.org/meta-binary-search-one-sided-binary-search/ | ||
| * | ||
| * Builds the index bit by bit from the most-significant bit to the least-significant bit. | ||
| * Works on sorted arrays and returns the index of target, or -1 if not found. | ||
| */ | ||
|
|
||
| function metaBinarySearch(sortedArray, target) { | ||
| if (!Array.isArray(sortedArray) || sortedArray.length === 0) { | ||
| return -1 | ||
| } | ||
|
|
||
| const n = sortedArray.length | ||
| const maxBit = Math.floor(Math.log2(Math.max(1, n - 1))) | ||
|
|
||
| let position = 0 | ||
| for (let bit = maxBit; bit >= 0; bit--) { | ||
| const candidate = position + 2 ** bit | ||
| if (candidate < n && sortedArray[candidate] <= target) { | ||
| position = candidate | ||
| } | ||
| } | ||
|
|
||
| return sortedArray[position] === target ? position : -1 | ||
| } | ||
|
|
||
| export { metaBinarySearch } |
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,28 @@ | ||
| import { metaBinarySearch } from '../MetaBinarySearch' | ||
|
|
||
| describe('Meta Binary Search', () => { | ||
| test('returns index for found numeric element', () => { | ||
| expect(metaBinarySearch([2, 5, 8, 12, 16, 23, 38], 23)).toBe(5) | ||
| }) | ||
|
|
||
| test('returns -1 when numeric element is missing', () => { | ||
| expect(metaBinarySearch([2, 5, 8, 12, 16, 23, 38], 9)).toBe(-1) | ||
| }) | ||
|
|
||
| test('works with sorted strings', () => { | ||
| expect(metaBinarySearch(['alpha', 'beta', 'delta', 'omega'], 'delta')).toBe( | ||
| 2 | ||
| ) | ||
| }) | ||
|
|
||
| test('returns index for a matching element in a single-element array', () => { | ||
| expect(metaBinarySearch([7], 7)).toBe(0) | ||
| }) | ||
| test('returns -1 on empty input', () => { | ||
| expect(metaBinarySearch([], 1)).toBe(-1) | ||
| }) | ||
|
|
||
| test('returns index for a matching element in a single-element array', () => { | ||
| expect(metaBinarySearch([7], 7)).toBe(0) | ||
| }) | ||
| }) | ||
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.