Created
October 31, 2014 18:01
-
-
Save Cee/526d4e89d84b3ba22306 to your computer and use it in GitHub Desktop.
BinarySearch
This file contains hidden or 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
int search(int array[], int n, int value) { | |
int left = 0; | |
int right = n - 1; | |
while (left <= right) { | |
int mid = left + ((right - left) >> 1); | |
if (array[mid] > value) { | |
right = mid - 1; | |
} else if (array[mid] < value) { | |
left = mid + 1; | |
} else { | |
return mid; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment