Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary Search
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.35 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.49 sec
edit mode
|
history
|
discussion