赞
踩
- class HalfSearch {
-
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
- int index = halfSearch(arr, 8);
- System.out.println(index);
-
- }
- public static int halfSearch(int[] arr, int target) {
- int max = arr.length - 1;
- int min = 0;
- int mid = (max + min) / 2;
- while(max >= min) {
- if(target > arr[mid]) {
- min = mid + 1;
- } else if(target < arr[mid]) {
- max = mid - 1;
- } else {
- return mid;
- }
- mid = (max + min) / 2;
- }
- return -1;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。