Created
February 27, 2012 06:06
-
-
Save dmjio/1921823 to your computer and use it in GitHub Desktop.
BinarySearch
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
static int BinarySearch(int[] arr, int x) | |
{ | |
int i = 0; | |
int m = 0; | |
int j = arr.Length - 1; | |
while (i < j) | |
{ | |
m = ((i+j)/2); | |
if (x > arr[m]) | |
i = m + 1; | |
else | |
j = m; | |
} | |
if (arr[m] == x) | |
return i; | |
else | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment