Skip to content

Instantly share code, notes, and snippets.

@yucombinator
Created December 15, 2014 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yucombinator/ef22ad19158eb82ced80 to your computer and use it in GitHub Desktop.
Save yucombinator/ef22ad19158eb82ced80 to your computer and use it in GitHub Desktop.
Iterative binary search
int binarySearchIte(int *a, int start, int end, int value) {
while ((end - start) > 1) {
int cursor = start + (end - start) / 2;
if (a[cursor] == value) {
return cursor;
}
if (a[cursor]<value) {
start = cursor;
}
else if (a[cursor]>value) {
end = cursor;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment