二分法查询算法:
* 二分法查找(折半查找)
*/
package cn.wu.array;
import java.util.Arrays;
public class BinarySearch {
public static void main(String[]args){
int[] arr = {30,20,50,10,80,9,7,12,100,40,8};
int searchWord = 20;
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println(searchWord+"元素的索引:"
+binarySearch(arr,searchWord));
}
public static int binarySearch(int[]array,int value){
int low = 0;
int high = array.length - 1;
while(low <= high){
int middle = (low + high)/2;
if(value == array[middle]){
return middle;
}
if(value > array[middle]){
low = middle + 1;
}
}
return -1;
}
}