public class Lx05
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5,6,7,8,9};
int a = show(arr,6);
System.out.println("6对应的角标是:"+a);
}
public static int show(int[] arr, int num)
{
int a = 0 ;
int b = arr.length-1;
int c = (a+b)/2;
while (arr[c]!=num)
{
if (arr[c]>num);
b=c-1;
if (arr[c]<num);
a = c+1;
if (a>b)
return -1;
c=( a+b)/2;
}
return c;
}
} |
|