The algorithm is easy. Attaches directly to the code it
#include <iostream>using namespace Std;template<typename t>int binary_search (const T s[],const int size, Const T &m) {if (size <= 0) {return-1;} int i = SIZE/2; int k = 1; int step = 1; while (step > 0 && I & gt;= 0 && i < size) {step = size/(2*++k); if (M = = S[i]) {return i; }else if (M > s[i]) {i + = step; }else{I-= step; }} return-1;} int main () {long s[]={1,3,5,6,19,27,38,45,687,6789,10000}; int len = sizeof (s)/sizeof (long); cout<< "0:" <<binary_search<long> (s,len,1) <<endl; cout<< "1:" <<binary_search<long> (s,len,3) <<endl; cout<< "5:" <<binary_search<long> (s,len,27) <<endl; cout<< ":" <<binary_search<long> (s,len,10000) <<endl; cout<< "-1:" <<binary_search<long> (s,len,10001) <<endl; cout<< "-1:" <<binary_search<long> (s,len,44) <<endl; cout<< "-1:" <<binary_search<long> (s,len,0) <<Endl return 0;}
Binary Search method C + + generic execution