Formula to find median in Binary Search changed#2181
Merged
realDuYuanChao merged 3 commits intoTheAlgorithms:masterfrom Apr 14, 2021
Merged
Formula to find median in Binary Search changed#2181realDuYuanChao merged 3 commits intoTheAlgorithms:masterfrom
realDuYuanChao merged 3 commits intoTheAlgorithms:masterfrom
Conversation
realDuYuanChao
approved these changes
Apr 14, 2021
Member
realDuYuanChao
left a comment
There was a problem hiding this comment.
Thanks for your contribution.
nirvanarsc
reviewed
Apr 15, 2021
|
|
||
| // find median | ||
| int median = (left + right) >>> 1; | ||
| int median = left + ((right-left) >>> 1); |
There was a problem hiding this comment.
Actually this change is not necessary. Above expression is already using a logical shift (three >>>) which will preserve the sign bit if a potential overflow happens when summing (left + right). For languages like C++ and others that only support arithmetic shift (two >>) above workaround is necessary.
TL;DR original code works fine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Finding median using the previous formula may carry the median value out of range. Therefore, updated the formula with a better one.
References
Checklist:
Fixes: #{$ISSUE_NO}.