Skip to content

fix: improve binary search mid calculation to avoid overflow#1891

Open
satish-sharma360 wants to merge 1 commit intoTheAlgorithms:masterfrom
satish-sharma360:patch-1
Open

fix: improve binary search mid calculation to avoid overflow#1891
satish-sharma360 wants to merge 1 commit intoTheAlgorithms:masterfrom
satish-sharma360:patch-1

Conversation

@satish-sharma360
Copy link
Copy Markdown

@satish-sharma360 satish-sharma360 commented Apr 8, 2026

Describe your change:

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

Improved the binary search implementation by updating the mid calculation.

Old approach:
mid = Math.floor((low + high) / 2)

New approach:
mid = low + Math.floor((high - low) / 2)

This prevents potential overflow issues and follows best practices used in production-level code.


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.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames follow PascalCase naming convention.
  • Added reference URL in comments.

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1891   +/-   ##
=======================================
  Coverage   85.91%   85.91%           
=======================================
  Files         379      379           
  Lines       19778    19779    +1     
  Branches     3016     3016           
=======================================
+ Hits        16993    16994    +1     
  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.

2 participants