Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (30 sloc) 745 Bytes
/**
Jorge Alejandro Jimenez Luna
Algorithm: Binary Search
*/
#include <bits/stdc++.h>
using namespace std;
int BinarySearch (int arr[], int _start, int _end, int item) {
if(_start == _end)
return -1;
int S = _start, E = _end;
int piv = 0;
while(arr[piv] != item) {
piv = (S+E)/2;
if(arr[piv] > item)
E = piv - 1;
if(arr[piv] < item)
S = piv + 1;
}
if(arr[piv] != item)
return -1;
return piv;
}
int main()
{
freopen("search.in", "r", stdin);
freopen("search.out", "w", stdout);
int sol[10] = {1, 3, 5, 7, 9, 11, 13, 16, 19, 21};
cout << BinarySearch(sol, 0, 9, 21);
return 0;
}