⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarysearch.java

📁 JAVA程序设计导论那本书上的一些源代码. 在学那本书的下来的
💻 JAVA
字号:
public class BinarySearch {  /** Main method */  public static void main(String[] args) {    int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};    int i = binarySearch(list, 2);    int j = binarySearch(list, 11);    int k = binarySearch(list, 12);    System.out.println(i);    System.out.println(j);    System.out.println(k);  }  /** Use binary search to find the key in the list */  public static int binarySearch(int[] list, int key) {    int low = 0;    int high = list.length - 1;    while (high >= low) {      int mid = (low + high) / 2;      if (key < list[mid])        high = mid - 1;      else if (key == list[mid])        return mid;      else        low = mid + 1;    }    return -low - 1;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -