Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int b_search(vector<int> a, int val) {
- int l = 0, r = a.size() - 1;
- while (r > l) {
- int mid = (l + r) / 2;
- if (a[mid] < val)
- l = mid + 1;
- else if (a[mid] > val)
- r = mid - 1;
- else
- return mid;
- }
- return l;
- }
- int main() {
- vector<int> a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- cout << b_search(a, 4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement