脑海中想到的第一种二分法的java实现方式
package com.abyss.px;
import java.util.Arrays;
public class NumbetTest {
public static void main(String[] args) {
int [] nums = new int[]{1,23,123,415,123,23123,61,23,12};
Arrays.sort(nums);
int count = 0;
for(int num :nums){
System.out.print(count+":"+num+"\t");
count++;
}
System.out.println("");
int orderNum = DicFind(415, nums);
System.out.println(orderNum);
}
private static int DicFind(int value,int[] sortedNums){
int begin = 0;
int end = sortedNums.length-1;
while(begin <= end){
int mid = (begin+end)/2;
if(sortedNums[mid] == value){
return mid;
}else if(sortedNums[mid] > value){
end = mid -1;
}else{
begin = mid +1;
}
}
return -1;
}
}
下方为结果截图