Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
Binary search2
//Title of this code #include <iostream> #include <vector> using namespace std; template<class T> int bin_search(vector<T> t, T val) { int i = 0; int j = t.size() - 1; while (i != j) { int k = i + (j - i) / 2; cout << i << " - " << k << " - " << j << endl; if (t[k] < val) i = k + 1; else if (t[k] > val) j = k - 1; else return k; } if (t[i] == val) return i; return -1; } int main() { vector<double> t; for (int i = 0; i < 100000; ++i) t.push_back(i*3.2); int p = bin_search<double>(t, 10000); cout << p << " " << t[p]; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
cpp ex 3 - solution
O
Stock buy/sell, maximum subarray problem
begin_end.cpp
fcyyfc
Building squares using smallest amount of matches
Overland pg. 68
BLREDSET
Coin changes
ThreadPool
Please log in to post a comment.