Created
January 19, 2016 12:34
-
-
Save sudipto80/41e44b86588e2d46db1d to your computer and use it in GitHub Desktop.
Binary Search
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
void Main() | |
{ | |
BinarySearch(new int[]{1,3,4,5,9,10},1).Dump(); | |
} | |
int BinarySearch(int[] array,int x) | |
{ | |
int low = 0; | |
int high = array.Length - 1; | |
while (low <= high) | |
{ | |
int mid = low + (high - low)/2; | |
if(array[mid]==x) | |
return mid; | |
if(array[mid]<x) | |
{ | |
low = mid + 1; | |
} | |
if(array[mid]>x) | |
{ | |
high = mid - 1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment