void binary_search_vec(std::vector<int> a, int finder) {
auto full_beg = a.begin();
auto beg = a.begin();
auto end = a.end();
auto mid = beg + (end - beg) / 2;
while (*mid != finder && mid != end) {
if (finder > *mid) {
beg = mid + 1;
}
else { //(*mid > finder)
end = mid;
}
mid = beg + (end - beg) / 2;
if (finder == *mid) { // Here is the problem leading to undefined behavior. I am negating the condition in the While Loop. Credit to Bob___ for the fix. And thank you to the community.
std::cout << "Num found: " << finder << " at position: " << std::distance(full_beg, mid) <<
std::endl;
break;
}
}
}
Edit: The code in question is from C++ Primer 5th edition. The binary search in question simply does not return the num found. I Can not understand why. Compared line by line with other users. Theirs work mine does not.
This code from another user works. I do not understand the difference 2nd edit: @Bob___ fix code:
auto full_beg = a.begin();
auto beg = a.begin();
auto end = a.end();
auto mid = beg + (end - beg) / 2;
while (*mid != finder && mid != end) {
if (finder > * mid) {
beg = mid + 1;
}
else { //(*mid > finder)
end = mid;
}
mid = beg + (end - beg) / 2;
}
if (finder == *mid) {
std::cout << "Num found: " << finder << " at position Index: " << std::distance(full_beg, mid) << std::endl;
}
else std::cout << "Not Found!" << std::endl;
}
Move the last if
branch out of the while loop and remove the break
statement otherwise it won't work if the finder
is the same as the mid
.
The following code works:
#include <iostream>
#include <vector>
void binary_search_vec(std::vector<int> a, int finder) {
auto full_beg = a.begin();
auto beg = a.begin();
auto end = a.end();
auto mid = beg + (end - beg) / 2;
std::cout << "MID AT THE BEGINNING: " << *mid << std::endl;
while (mid != end && *mid != finder) {
if (finder > *mid) {
std::cout << "Finder > mid" << std::endl;
beg = mid + 1;
}
else { //(*mid > finder)
std::cout << "Finder > mid" << std::endl;
end = mid;
}
mid = beg + (end - beg) / 2;
std::cout << "Setting mid to " << *mid << std::endl;
}
if (finder == *mid) {
std::cout << "Num found: " << finder << " at position: " << std::distance(full_beg, mid) << std::endl;
}
}
int main() {
std::cout << "Hello World!\n";
std::vector<int> v = std::vector<int>();
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
binary_search_vec(v, 1);
}
Using your function, the previous code does not work when finder = 2
.
Also check the comment from @Bob__: the order of the condition evaluation inside the while is extremely important!
Here is the fix and updated code. Courtesy of Bob___
void binary_search_vec(std::vector a, int finder) {
auto full_beg = a.begin();
auto beg = a.begin();
auto end = a.end();
auto mid = beg + (end - beg) / 2;
while (*mid != finder && mid != end) {
if (finder > * mid) {
beg = mid + 1;
}
else { //(*mid > finder)
end = mid;
}
mid = beg + (end - beg) / 2;
}
if (finder == *mid) {
std::cout << "Num found: " << finder << " at position Index: " << std::distance(full_beg, mid) << std::endl;
}
else std::cout << "Not Found!" << std::endl;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.