feat: add meta binary search implementation #1894
新建议题
Have a question about this project? 注册 for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “注册 for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? 登录 to your account
Closed
nickzerjeski
wants to merge
4
commits into
TheAlgorithms:master
from
nickzerjeski:feat-meta-binary-search-1835
Closed
Changes from all commits
提交
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 搜索 | ||
| * 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 metaBinary搜索(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 { metaBinary搜索 } |
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 { metaBinary搜索 } from '../MetaBinary搜索' | ||
|
|
||
| describe('Meta Binary 搜索', () => { | ||
| test('returns index for found numeric element', () => { | ||
| expect(metaBinary搜索([2, 5, 8, 12, 16, 23, 38], 23)).toBe(5) | ||
| }) | ||
|
|
||
| test('returns -1 when numeric element is missing', () => { | ||
| expect(metaBinary搜索([2, 5, 8, 12, 16, 23, 38], 9)).toBe(-1) | ||
| }) | ||
|
|
||
| test('works with sorted strings', () => { | ||
| expect(metaBinary搜索(['alpha', 'beta', 'delta', 'omega'], 'delta')).toBe( | ||
| 2 | ||
| ) | ||
| }) | ||
|
|
||
| test('returns index for a matching element in a single-element array', () => { | ||
| expect(metaBinary搜索([7], 7)).toBe(0) | ||
| }) | ||
| test('returns -1 on empty input', () => { | ||
| expect(metaBinary搜索([], 1)).toBe(-1) | ||
| }) | ||
|
|
||
| test('returns index for a matching element in a single-element array', () => { | ||
| expect(metaBinary搜索([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.