#include <iostream>
using namespace std;
void divide(int lo, int hi, int &m) {
m = (lo + hi) >> 1;
}
//solve recursively
void searchbin(int vec[], int left, int right, int x, int &pos) {
int middle;
if(left <= right) {
//truncate the interval
divide(left, right, middle);
//compare the middle element
if( vec[ middle ] == x) { pos = middle; }
else if(x < vec[middle]) searchbin(vec, left, middle - 1, x, pos);
else
searchbin(vec, middle + 1, right, x, pos);
}
}
//solve recursively
int binsearch(int vec[], int lo, int hi, int x) {
if(lo > hi) return -1;
int m;
m = (lo + hi) >> 1;
if(vec[ m ] == x) return m + 1;
else if(vec[ m ] > x) return binsearch(vec, lo, m - 1, x);
else
return binsearch(vec, m + 1, hi, x);
}
//solve iteratively
int _searchbin(int vec[], int left, int right, int x) {
int pos = -1,
m;
while(left <= right) {
m = (left + right) >> 1;
if(vec[ m ] == x) {
pos = m;
break;
}
else if(vec[m] > x) {
right = m - 1;
} else {
left = m + 1;
}
}
return pos + 1;
}
int main(int argc, char const *argv[]) {
int vec[] = {17,33,73,75,78,90,91,92,100},
n,
r,
search,
pos = -1;
n = sizeof(vec)/sizeof(vec[0]);
cin>>search;
searchbin(vec, 0, n - 1, search, pos);
if(pos == -1) cout<<"Not Found!";
else
cout<<"Found [recursively] on position: " << pos+1;
cout<<endl;
r = binsearch(vec, 0, n - 1, search);
cout<<"[recursive] Find at position: "<<r<<endl;
r = _searchbin(vec, 0, n - 1, search);
if( r ) cout<<"[recursive] Find at position: "<<r<<endl;
else
cout<<"[iterative] Not Found!";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgZGl2aWRlKGludCBsbywgaW50IGhpLCBpbnQgJm0pIHsKCiAgICBtID0gKGxvICsgaGkpID4+IDE7Cn0KCi8vc29sdmUgcmVjdXJzaXZlbHkKdm9pZCBzZWFyY2hiaW4oaW50IHZlY1tdLCBpbnQgbGVmdCwgaW50IHJpZ2h0LCBpbnQgeCwgaW50ICZwb3MpIHsKCiAgICBpbnQgbWlkZGxlOwoKICAgIGlmKGxlZnQgPD0gcmlnaHQpIHsKCiAgICAgICAgLy90cnVuY2F0ZSB0aGUgaW50ZXJ2YWwKICAgICAgICBkaXZpZGUobGVmdCwgcmlnaHQsIG1pZGRsZSk7CgogICAgICAgIC8vY29tcGFyZSB0aGUgbWlkZGxlIGVsZW1lbnQKICAgICAgICBpZiggdmVjWyBtaWRkbGUgXSA9PSB4KSB7IHBvcyA9IG1pZGRsZTsgfQoKICAgICAgICBlbHNlIGlmKHggPCB2ZWNbbWlkZGxlXSkgc2VhcmNoYmluKHZlYywgbGVmdCwgbWlkZGxlIC0gMSwgeCwgcG9zKTsKCiAgICAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWFyY2hiaW4odmVjLCBtaWRkbGUgKyAxLCByaWdodCwgeCwgcG9zKTsKICAgIH0KICAgIAp9CgovL3NvbHZlIHJlY3Vyc2l2ZWx5CmludCBiaW5zZWFyY2goaW50IHZlY1tdLCBpbnQgbG8sIGludCBoaSwgaW50IHgpIHsKCiAgICBpZihsbyA+IGhpKSByZXR1cm4gLTE7CgogICAgaW50IG07CgogICAgbSA9IChsbyArIGhpKSA+PiAxOwoKICAgIGlmKHZlY1sgbSBdID09IHgpIHJldHVybiBtICsgMTsKCiAgICBlbHNlIGlmKHZlY1sgbSBdID4geCkgcmV0dXJuIGJpbnNlYXJjaCh2ZWMsIGxvLCBtIC0gMSwgeCk7CgogICAgICAgICAgICAgICAgICBlbHNlCgogICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBiaW5zZWFyY2godmVjLCBtICsgMSwgaGksIHgpOwp9CgovL3NvbHZlIGl0ZXJhdGl2ZWx5CmludCBfc2VhcmNoYmluKGludCB2ZWNbXSwgaW50IGxlZnQsIGludCByaWdodCwgaW50IHgpIHsKCiAgICAgaW50IHBvcyA9IC0xLAogICAgICAgICBtOwoKICAgICB3aGlsZShsZWZ0IDw9IHJpZ2h0KSB7CgogICAgICAgICAgICBtID0gKGxlZnQgKyByaWdodCkgPj4gMTsKICAgICAgICAgICAgCiAgICAgICAgICAgaWYodmVjWyBtIF0gPT0geCkgewoKICAgICAgICAgICAgICAgICAgIHBvcyA9IG07CiAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICB9CgogICAgICAgICAgIGVsc2UgaWYodmVjW21dID4geCkgewoKICAgICAgICAgICAgICAgICAgIHJpZ2h0ID0gbSAtIDE7CgogICAgICAgICAgIH0gZWxzZSB7CgogICAgICAgICAgICAgICAgICAgbGVmdCA9IG0gKyAxOwogICAgICAgICAgIH0KICAgICB9CgogICAgIHJldHVybiBwb3MgKyAxOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogIGludCB2ZWNbXSA9IHsxNywzMyw3Myw3NSw3OCw5MCw5MSw5MiwxMDB9LAogICAgICBuLAogICAgICByLAogICAgICBzZWFyY2gsCiAgICAgIHBvcyA9IC0xOwogIG4gPSBzaXplb2YodmVjKS9zaXplb2YodmVjWzBdKTsKICAKICBjaW4+PnNlYXJjaDsKCiAgc2VhcmNoYmluKHZlYywgMCwgbiAtIDEsIHNlYXJjaCwgcG9zKTsKCiAgaWYocG9zID09IC0xKSBjb3V0PDwiTm90IEZvdW5kISI7CgogICAgICAgICBlbHNlCgogICAgICAgICAgICAgICBjb3V0PDwiRm91bmQgW3JlY3Vyc2l2ZWx5XSBvbiBwb3NpdGlvbjogIiA8PCBwb3MrMTsKCiAgY291dDw8ZW5kbDsKICByID0gYmluc2VhcmNoKHZlYywgMCwgbiAtIDEsIHNlYXJjaCk7CiAgY291dDw8IltyZWN1cnNpdmVdIEZpbmQgYXQgcG9zaXRpb246ICI8PHI8PGVuZGw7CgogIHIgPSBfc2VhcmNoYmluKHZlYywgMCwgbiAtIDEsIHNlYXJjaCk7CiAgaWYoIHIgKSAgIGNvdXQ8PCJbcmVjdXJzaXZlXSBGaW5kIGF0IHBvc2l0aW9uOiAiPDxyPDxlbmRsOwogICAgICAgIGVsc2UgCiAgICAgICAgICAgY291dDw8IltpdGVyYXRpdmVdIE5vdCBGb3VuZCEiOwogIHJldHVybiAwOwp9