public class erchachazhao {
public static void print(int[] array,int middle){
int i;
for( i=0;i<array.length;i++){
System.out.print(array[i]);
if (i==middle){
System.out.print("*");
}
System.out.print(" ");}
System.out.println();
}
public static int indexof(int[] array,int value){
int low=0;
int high=array.length-1;
int middle;
while(low<=high)
{ middle=(low+high)/2;
print(array,middle);
if(value<array[middle]){
high=middle-1;
}else if(value==array[middle]){
return middle;
}else{
low=middle+1;
}
}
return -1;
}
public static void main(String[] args){
int[] a=new int[]{2,4,9,13,23,36,46,58,78,94};
System.out.println("location of 46 is:"+indexof(a,46));
System.out.println("location of 9 is:"+indexof(a,9));
}
}
public static void print(int[] array,int middle){
int i;
for( i=0;i<array.length;i++){
System.out.print(array[i]);
if (i==middle){
System.out.print("*");
}
System.out.print(" ");}
System.out.println();
}
public static int indexof(int[] array,int value){
int low=0;
int high=array.length-1;
int middle;
while(low<=high)
{ middle=(low+high)/2;
print(array,middle);
if(value<array[middle]){
high=middle-1;
}else if(value==array[middle]){
return middle;
}else{
low=middle+1;
}
}
return -1;
}
public static void main(String[] args){
int[] a=new int[]{2,4,9,13,23,36,46,58,78,94};
System.out.println("location of 46 is:"+indexof(a,46));
System.out.println("location of 9 is:"+indexof(a,9));
}
}