class Program { static void Main(string[] args) { int[] arr = new int[] { 5,10,50,200,1000,10001}; int index = BinarySearch(arr, 5, 5, 10001); } static int BinarySearch(int[] array, int left, int right, int value) { int mid; while (left <= right) //Important to use <= { mid = ((left + right) >> 1); if (array[mid] == value) return mid; else if (array[mid] < value) left = mid + 1; else if (array[mid] > value) right = mid - 1; } return - 1; } }