Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary Search
#include <iostream> using namespace std; // Parameters: (Array, Start index, End index, Element to be searched) int binarySearch(int a[], int l, int r, int key) { while(l <= r) { int mid = l+(r-l)/2; if(a[mid] == key) return mid; else if(a[mid] > key) r = mid-1; else l = mid+1; } return -1; } int main() { int n = 10; int a[n] = {-5, -2, 11, 13, 21, 25, 30, 34, 38, 40}; // Sorted array int index = binarySearch(a, 0, n-1, 30); if(index != -1) cout << "-5 is present at index: " << index << "\n\n"; else cout << "Element is not present in the array\n\n"; return 0; }
run
|
edit
|
history
|
help
0
Kalkulator z bajerami
Boost phoenix. e.g 2: functor
Big O Notation array problem
test
Array Subset of another array
Minimum Vertices to Traverse Directed Graph
Goooood
UtilityPair2
Iviewb
shell sort