Skip to content

Commit

Permalink
Fixed the underFlow bug in src/binarySearch.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya authored and Aditya committed Oct 2, 2023
1 parent 1f48039 commit 9100078
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Searches/BinarySearch.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ contract BinarySearch {
uint256 currentElement = _nums[middle];
if (currentElement < value) {
minimum = middle + 1;
} else if (currentElement > value) {
maximum = middle - 1;
}
else if (currentElement > value) {
if (middle == 0) {
maximum = 0;
} else {
maximum = middle - 1;
}
} else {
result = middle;
result = int256(middle);
// return the index position of the value in the array
return result;
}
}
// return 0 if value is not in the array
return 0;
return -1;
}
}

0 comments on commit 9100078

Please sign in to comment.