publicclassSearch{publicstaticvoidmain(String[]args){inta[]={1,3,5,7,8,9,12,14,15,17};inti=9;System.out.println(search(a,i));//System.out.println(binarySearch(a,i));}publ...
public class Search {
public static void main(String[] args) {
int a[] = {1,3,5,7,8,9,12,14,15,17};
int i = 9;
System.out.println(search(a, i));
//System.out.println(binarySearch(a,i));
}
public static int search(int[] a, int num) {
for(int i=0; i
if(a[i] == num) return i;
}
return -1;
}
}
/*public static int binarySearch(int[] a,int num) {
if (a.length == 0) return -1;
int startPos = 0;
int endPos = a.length-1;
int m = (startPos + endPos) / 2;
while(startPos <= endPos) {
if(a[m] == num) return m;
if(num > a[m]) {
startPos = m+1;
}
if(num < a[m]) {
endPos = m-1;
}
m = (startPos + endPos)/2;
}
return -1;
}*/
}
我晕,看掉了最后那个大括号,艹
展开