急!!!!!JAVA编程题

2025-03-30 09:18:49
推荐回答(2个)
回答1:

public class test1 {
public static void main(String[] args) {

int a[] = {2,4,6,9,12,56,89,100,123,567};
int i = 100;
System.out.println(binarysearch(a,i));

}

public static int binarysearch(int a[],int number){
if(a.length==0)
return -1;

int startpos =0;
int endpos = a.length-1;
int m = (startpos + endpos)/2;
while (startpos<=endpos){
if(number==a[m])
return m+1;
if(number>a[m]){
startpos = m+1;
}
if(number endpos = m-1;
m = (startpos+endpos)/2;
}
return -1;
}
}

你可以借鉴下 有什么不清楚 可以Hi我

回答2:

你这样的是一个数组排序。