Last active
February 27, 2016 20:33
Binary search in the circular array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
bool search_list(int* list, int s, int e, int target) | |
{ | |
if (s > e) | |
return false; | |
int h = s + (e-s)/2; | |
if (target == list[h]) | |
return true; | |
if (target < list[h]) | |
{ | |
if (target >= list[s]) | |
e = h-1; | |
else | |
s = h +1; | |
} else { | |
if (target <= list[e]) | |
s = h+1; | |
else | |
e = h-1; | |
} | |
return search_list(list, s, e, target); | |
} | |
int main () | |
{ | |
int input[8] = {4,5,6,7,8,1,2,3}; | |
cout<<" finding 3 = "<<search_list(input, 0, 7, 3)<<endl; | |
cout<<" finding 5 = "<<search_list(input, 0, 7, 5)<<endl; | |
cout<<" finding 9 = "<<search_list(input, 0, 7, 9)<<endl; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment