Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Binary Search
//Binary Search #include <iostream> #include <vector> using namespace std; int bin_search(vector<int>* t, int num, int i, int j) { if (i == j){ if (t->at(i) == num) return i; else return -1; } int mid = i + ((j - i) / 2); if (t->at(mid) == num) return mid; if (num < t->at(mid)) return bin_search(t, num, i, mid - 1); else return bin_search(t, num, mid + 1, j); } int main() { cout << "Binary search!\n"; vector<int>* t = new vector<int>(); t->push_back(6); t->push_back(7); t->push_back(8); t->push_back(9); int v = bin_search(t, 8, 0, t->size() - 1); cout << v << endl; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
-Wall -std=c++14 -O0 -o a.out source_file.cpp
GCC compiler bug (should output 0 1)
Dar
Treap (making range queries(that are not possible on seg_Trees) possible with no effort) : (863D)
Six Trigonometric Functions
Template HeapSort
shell sort
C++ - Chained Methods
Building squares using smallest amount of matches
Addition of two matrix **Part 2
stackse - search stackoverflow differently
Please log in to post a comment.