Skip to content
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

Avoid possible overflow (theoretical) in the example #28

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from

Conversation

Bolshem
Copy link

@Bolshem Bolshem commented Sep 22, 2020

Avoid possible overflow if the array size is greater than (MAX_INT / 2) and the searched item is one of the lasts.

I understand this is totally theoretical, but this seems as the right way to find a middle.

Avoid possible overflow if array size is greater than (MAX_INT / 2) and searched item is one of the lasts.
@Bolshem Bolshem changed the title Update BinarySearch.sc Avoid possible overflow (theoretical) in the example Sep 22, 2020
@@ -3,7 +3,7 @@ def binarySearch[T: Ordering](sorted: IndexedSeq[T], target: T): Boolean = {
// If the sequence you are trying to search has no items, the item cannot be found
if (start == end) false
else {
val middle = (start + end) / 2
val middle = start + (end - start) / 2

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants