Skip to content

feat(collections/unstable): add non-exact binary search function #6628

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lionel-rowe
Copy link
Contributor

Towards #3069.

Note that this adds a new binarySearch function under collections, which operates on array-likes of sorted numbers/bigints/strings and implements the same semantics as C#'s Array.BinarySearch and Java's Arrays.binarySearch. It doesn't patch the existing BinarySearchTree under data-structures (@WWRS has a separate open PR for that: #6544).

assertEquals(binarySearch([0, 1], 0), 0)
assertEquals(binarySearch([0, 1], 1), 1)
assertEquals(binarySearch([0, 1], -0.5), -1) // (bitwise complement of 0)
assertEquals(binarySearch([0, 1], 0.5), -2) // (bitwise complement of 1)
assertEquals(binarySearch([0, 1], 1.5), -3) // (bitwise complement of 2)

Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.74%. Comparing base (096f0be) to head (cb26eda).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6628   +/-   ##
=======================================
  Coverage   94.74%   94.74%           
=======================================
  Files         583      584    +1     
  Lines       46478    46497   +19     
  Branches     6523     6527    +4     
=======================================
+ Hits        44036    44055   +19     
  Misses       2399     2399           
  Partials       43       43           

☔ 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.

Comment on lines +7 to +10
* Binary searching may be preferable to `Array#findIndex` if the array is
* large and performance is at a premium, or if information about the insertion
* index is needed upon non-exact matches (`Array#findIndex` simply returns
* `-1` in such cases).
Copy link
Member

Choose a reason for hiding this comment

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

👍

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

Successfully merging this pull request may close these issues.

2 participants