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

binary search fix #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cpp/include/algorithm/searching/binary_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
template <typename T>
int binary_search(const T& value, const std::vector<T>& sorted_values,
const int low, const int high) {

int mid = low + (high - low) / 2;

if (value == sorted_values[mid])
return mid;
else if (low <= high) {
else if (low < high) {
if (value < sorted_values[mid]) {
// value must be between indices low and mid-1, if exists
return binary_search(value, sorted_values, low, mid - 1);
Expand Down
Loading