Skip to content

Instantly share code, notes, and snippets.

@agasiev
Created December 26, 2012 13:42
Show Gist options
  • Save agasiev/4380414 to your computer and use it in GitHub Desktop.
Save agasiev/4380414 to your computer and use it in GitHub Desktop.
Binary search
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int bsearch(vector<int> a, int left, int right, int what) {
if (right < left) return -1;
int middle = left + (right - left)/2;
if (a[middle] < what) {
return bsearch(a, left, middle-1, what);
}
else if (a[middle] > what) {
return bsearch(a, middle + 1, right, what);
}
return middle;
}
int main(int, char**) {
vector<int> a(10);
for (int i = 0; i < a.size(); i++) {
a[i] = 10 - i;
cout << a[i] << " ";
}
cout << endl;
for (int i = 16; i >= 0; i--)
cout << i << " at " << bsearch(a, 0, a.size()-1, i) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment