public class BinarySearch { public static void main(String... args){ int[] array = new int[10]; for(int i=0; i<10;i++){ array[i] = i * 56; } System.out.println(binarySearch(array, 112)); } public static int binarySearch(int[] array, int value) { int low = 0; int high = array.length - 1; while (low < high) { int mid = (low + high) / 2; if (array[mid] > value) { high = mid - 1; } else if (array[mid] < value) { low = mid + 1; } else { return mid; } } return -1; } }
No comments:
Post a Comment