基本原理:每次查找都对半分,但要求数组是有序的
1 public class Solution { 2 3 public static int BinarySearch(int[] sz,int key){ 4 int low = 0; 5 int high = sz.length - 1; 6 7 while (low <= high) { 8 int middle = (low + high) / 2; 9 if(sz[middle] == key){ 10 return middle; 11 }else if(sz[middle] > key){ 12 high = middle - 1; 13 }else { 14 low = middle + 1; 15 } 16 } 17 return -1; 18 } 19 }