Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
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
uuuu
PLoshtina na krug
Stock buy/sell, maximum subarray problem
Изволов#7
Find the row with max ones
lifff
strcpy
inheritance test
Fundamentos de programación. Tema 7. Ejercicio 6. Con funciones.
2015(M2)Simulare