feat: add meta binary search implementation#1894
feat: add meta binary search implementation#1894nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
Conversation
There was a problem hiding this comment.
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
metaBinarySearchimplementation inSearch/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.
Search/MetaBinarySearch.js
Outdated
| } | ||
|
|
||
| const n = sortedArray.length | ||
| const maxBit = Math.floor(Math.log2(n - 1 < 0 ? 0 : n - 1)) |
There was a problem hiding this comment.
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).
| const maxBit = Math.floor(Math.log2(n - 1 < 0 ? 0 : n - 1)) | |
| const maxBit = Math.floor(Math.log2(Math.max(1, n - 1))) |
Search/MetaBinarySearch.js
Outdated
|
|
||
| let position = 0 | ||
| for (let bit = maxBit; bit >= 0; bit--) { | ||
| const candidate = position | (1 << bit) |
There was a problem hiding this comment.
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.
| const candidate = position | (1 << bit) | |
| const candidate = position + 2 ** bit |
| 2 | ||
| ) | ||
| }) | ||
|
|
There was a problem hiding this comment.
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.
| test('returns index for a matching element in a single-element array', () => { | |
| expect(metaBinarySearch([7], 7)).toBe(0) | |
| }) |
89dda91 to
0de1aa1
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Describe your change:
Added
Search/MetaBinarySearch.jswith tests inSearch/test/MetaBinarySearch.test.js.This contributes to the issue request for additional searching/sorting algorithm coverage.
Checklist:
Fixes: #{$ISSUE_NO}.Tests
npm test -- MetaBinarySearchFixes #1835