package m;
import java.util.*;
public class feishi {
public static int[]data={1,0,9,7,55,33,22,13,64,80};
public static void main(String[]args)
{
System.out.println("enter the key you will find:");
Scanner scan=new Scanner(System.in);
int key=scan.nextInt();
f(key,0,9);
}
public static boolean f(int k,int l,int h)
{
int key=k;
int low=l;
int high=h;
int mid=low+(key-data[low])*(high-low)/(data[high]-data[low]);
if(mid<low)
mid=low;
if(mid>high)
mid=high;
while(low<=high)
{
if(key<data[mid])
high=mid-1;
else if(key>data[mid])
low=mid+1;
else if(key==data[mid])
{
System.out.println("the data is in the array "+(mid+1));
return true;
}
if(low<high)
{
mid=low+(key-data[low])*(high-low)/(data[high]-data[low]);
}
if(mid<low)
mid=low;
if(mid>high)
mid=high;
}
return false;
}
}
本文出自 “hagar” 博客,谢绝转载!