[AppleScript] 纯文本查看 复制代码 package com.heima.array;
public class Demo_Biinary {
/**
* * 数组高级二分查找代码
*/
public static void main(String[] args) {
int [] arr = {11,22,33,44,55,66,77,88,99};
System.out.println(getIndex(arr,25));
}
//数组高级查找之二分法;
/*
* 分析:
* 1、明确参数列表;int[] arr,vaule;
* 2、明确返回值类型
* */
public static int getIndex(int [] arr,int vaule){
int min = 0; //定义最小索引
int max = arr.length-1;//定义最大索引值
int mid =(min+max)/2;//定义中间索引值
while(arr[mid] != vaule){
if(arr[mid]<vaule){
min = mid+1;//这里啊,你是有病啊,当传入的值比中间值还小时肯定是,是再从中间值的下一个值往后找啊
}else if(arr[mid]>vaule){
max = mid - 1;
}
mid =(max+min)/2;
if(min>max){
return -1;
}
}
return mid;
}
}
|