一.二分查找
1.非递归实现
索引 while
left<=right
1 public static int binarySearch(int[] arr,int key) { 2 int left=0; 3 int right=arr.length-1; 4 int mid; 5 while(left<=right) { // 小于等于<= 6 mid=(left+right)/2; 7 if(key==arr[mid]) 8 return mid; 9 if(key<arr[mid]) 10 right=mid-1; 11 else 12 left=mid+1; 13 } 14 return -1; //没找到返回-1 15 }