Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // For when normal binary search takes too many lines of code
- // when first called, lmin should be the least element, 0
- // and lmax should be v.size() - 1, and v should be sorted ascending
- template<class T>
- int recursiveBinarySearch(vector<T> v, T target, int lmin, int lmax){
- int middle = (lmin + lmax) / 2;
- lmin = v[middle] < target ? middle + 1 : lmin;
- lmax = v[middle] >= target ? middle - 1 : lmax;
- return v[middle] == target ? middle : lmin > lmax ? -1 : recursiveBinarySearch(v, target, lmin, lmax);
- }
Advertisement
Add Comment
Please, Sign In to add comment