Skip to content

Fix binary search #77

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

Merged
merged 1 commit into from
Aug 27, 2021
Merged

Conversation

MaximSmolskiy
Copy link
Member

Current binary search implementation has bug. For example,

>> binary_search(1:100, 30)
target is not found in aray
>> binary_search(1:100, 70)
target is not found in aray

The bug is in maximum iteration needed to find the target. There should be binary logarithm log2 instead of natural log. In case of natural logarithm log algorithm lacks iterations in some cases (e.g. in above). But in some cases it works properly. For example,

>> binary_search(1:100, 50)

ans =

    50

@cozek
Copy link
Member

cozek commented Aug 27, 2021

Thanks!

@cozek cozek merged commit 4dce5d1 into TheAlgorithms:master Aug 27, 2021
@MaximSmolskiy MaximSmolskiy deleted the fix_binary_search branch September 4, 2021 12:56
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