标签:return else 调用 int public java search out style
1 package cn.zhang.Array; 2 3 import java.util.Arrays; 4 5 /** 6 * 测试二分查找(折半检索) 7 * @author 张涛 8 * 9 */ 10 public class TestBinarySearch { 11 public static void main(String[] args){ 12 int[] a = {10,9,8,7,6,5,4,3,2,1}; 13 14 Arrays.sort(a);//先排序 15 System.out.println(Arrays.toString(a)); 16 17 System.out.println(myBinarySearch(a,1));//调用方法 18 19 } 20 21 public static int myBinarySearch(int[] arr,int value){ 22 23 int low = 0;//最小索引值 24 int high = arr.length - 1;//最大索引值 25 26 while(low<=high) { 27 int mid = (low + high) / 2; 28 29 if(arr[mid] == value) { 30 return mid; 31 } 32 else if(arr[mid]>value) { 33 high = mid - 1; 34 } 35 else if(arr[mid]<value) { 36 low = mid + 1; 37 } 38 } 39 return -1; 40 } 41 }
标签:return else 调用 int public java search out style
原文地址:https://www.cnblogs.com/zhangqiling/p/11376279.html