Java 二分查找

 1     public int binarySearch(int[] nums, int target) {
 2         int low = 0;
 3         int high = nums.length;
 4         while (low <= high) {
 5             int mid = (low + high) / 2;
 6             if (nums[mid] == target)
 7                 return mid;
 8             else if (nums[mid] < target)
 9                 low = mid + 1;
10             else
11                 high = mid - 1;
12         }
13         return -1;
14     }

 

posted @ 2016-08-22 17:05  Acode  阅读(148)  评论(0编辑  收藏  举报
您是本站第访问量位访问者!