Algorithm-java-BinarySearch

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { System.out.println("The java program include bug."); int[] number = {1, 4, 2, 6, 7, 3, 9, 8}; // BinarySearch.sort(number); int find = BinarySearch.search(number, 8); if(find != -1) System.out.println("找到數值於索引" + find); else System.out.println("找不到數值"); } } class BinarySearch { public static int search(int[] number, int des) { int low = 0; int upper = number.length - 1; while(low <= upper) { int mid = (low+upper) / 2; if(number[mid] < des) low = mid+1; else if(number[mid] > des) upper = mid - 1; else return mid; } return -1; } }
Editor Settings
Theme
Key bindings
Full width
Lines