private static Integer subSearch(int[] sourceInts,Integer targetInt,int minNum,int maxNum){
count++;
max=maxNum;
min=minNum;
half=(max+min)/2;
if(sourceInts[half]==targetInt){
return half;
}
else if(min==max || half==max || half==min){
return -1;
}
else if(sourceInts[half]>targetInt){
return subSearch(sourceInts,targetInt,min,half);
}
else{
return subSearch(sourceInts,targetInt,half,max);
}
}
private static boolean init(int[] sourceInt,Integer targetStr){
boolean flag=false;
Arrays.sort(sourceInt);
if(sourceInt[0]>targetStr){
System.out.println("不存在符合要求的数,因为过小");
}
else if(sourceInt[sourceInt.length-1]<targetStr){
System.out.println("不存在符合要求的数,因为过大");
}
else{
flag=true;
}
return flag;
}
public static void main(String[] args){
int searcher=9999;
int[] is=new int[10000];
Long time1=System.currentTimeMillis();
for(int i=0;i<is.length;i++){
is[i]=i;
System.out.println(is[i]);
}
Long time2=System.currentTimeMillis();
int num=search(is,searcher);
Long time3=System.currentTimeMillis();
System.out.println("下标:"+num);
System.out.println("查询次数"+count);
System.out.println("初始时间"+(time2-time1));
System.out.println("查询时间"+(time3-time2));
System.out.println("num:"+is[num]);
Long time4=System.currentTimeMillis();
for(int i=0;i<is.length;i++){
if(is[i]==searcher){
break;
}
}
Long time5=System.currentTimeMillis();
System.out.println("遍历时间"+(time5-time4));
}
}
柴油发电机
发电机
柴油机
柴油发电机
13636374743(上海)
13291526067(嘉兴)