Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary search on sorted array
//Binary search in sorted array #include <iostream> #include <vector> using namespace std; template<class T> int binSearch(vector<T>& t, T num) { int i = 0; int j = t.size() - 1; while (i != j) { int mid = i + (j - i) / 2; if (t[mid] < num) i = mid + 1; else if (t[mid] > num) j = mid - 1; else return mid; } if (t[i] == num) return i; return -1; } int main() { vector<int> t; t.push_back(-3); t.push_back(-2); t.push_back(0); t.push_back(4); t.push_back(8); int p = binSearch<int>(t, 8); cout << p; }
run
|
edit
|
history
|
help
0
543
12535
BadCastAllExcept
Test 5(2020)
Arithmetic
RegexSearch
Test 8(2010)
DP on Trees: Type B (In/out Dp)
Weighted Average
star print1