Skip to content

feat: add meta binary search implementation#1894

Open
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-meta-binary-search-1835
Open

feat: add meta binary search implementation#1894
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-meta-binary-search-1835

Conversation

@nickzerjeski
Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Added Search/MetaBinarySearch.js with tests in Search/test/MetaBinarySearch.test.js.
This contributes to the issue request for additional searching/sorting algorithm coverage.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Tests

  • npm test -- MetaBinarySearch

Fixes #1835

@nickzerjeski nickzerjeski requested a review from appgurueu as a code owner April 13, 2026 09:02
Copilot AI review requested due to automatic review settings April 13, 2026 09:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “Meta Binary Search” algorithm implementation to the Search/ algorithms collection, along with a dedicated Jest test suite, expanding coverage requested in #1835.

Changes:

  • Added metaBinarySearch implementation in Search/MetaBinarySearch.js.
  • Added Jest tests for numeric, string, missing-target, and empty-array cases in Search/test/MetaBinarySearch.test.js.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
Search/MetaBinarySearch.js Introduces the meta binary search implementation and its public export.
Search/test/MetaBinarySearch.test.js Adds a test suite validating core expected behavior for the new search algorithm.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

const n = sortedArray.length
const maxBit = Math.floor(Math.log2(n - 1 < 0 ? 0 : n - 1))
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxBit becomes -Infinity when n === 1 because Math.log2(0) is -Infinity. While the current loop happens to skip in that case, it’s brittle and makes maxBit non-integer. Consider handling n === 1 explicitly or computing maxBit in a way that never yields -Infinity (e.g., clamp to 0).

Suggested change
const maxBit = Math.floor(Math.log2(n - 1 < 0 ? 0 : n - 1))
const maxBit = Math.floor(Math.log2(Math.max(1, n - 1)))

Copilot uses AI. Check for mistakes.

let position = 0
for (let bit = maxBit; bit >= 0; bit--) {
const candidate = position | (1 << bit)
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation relies on bitwise operators (| and 1 << bit) to build indices. In JavaScript, bitwise ops coerce to signed 32-bit integers, so for arrays with length > 2^31 (or when bit >= 31) the computed candidate/position can overflow and become negative/incorrect. Prefer arithmetic (position + 2 ** bit) or a BigInt-based approach to keep indices correct up to the maximum array length.

Suggested change
const candidate = position | (1 << bit)
const candidate = position + 2 ** bit

Copilot uses AI. Check for mistakes.
2
)
})

Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests cover empty input and typical cases, but don’t cover the n === 1 case (which currently produces maxBit = -Infinity). Adding a single-element array test would lock in expected behavior and guard against regressions if the loop logic changes.

Suggested change
test('returns index for a matching element in a single-element array', () => {
expect(metaBinarySearch([7], 7)).toBe(0)
})

Copilot uses AI. Check for mistakes.
@nickzerjeski nickzerjeski force-pushed the feat-meta-binary-search-1835 branch from 89dda91 to 0de1aa1 Compare April 13, 2026 09:08
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.93%. Comparing base (5c39e87) to head (b2269fc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1894      +/-   ##
==========================================
+ Coverage   85.91%   85.93%   +0.01%     
==========================================
  Files         379      380       +1     
  Lines       19778    19806      +28     
  Branches     3016     3023       +7     
==========================================
+ Hits        16993    17021      +28     
  Misses       2785     2785              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Searching & Sorting Algorithms

3 participants