首页 > 编程语言 > 详细

java-插补查找法

时间:2014-03-16 01:19:13      阅读:368      评论:0      收藏:0      [点我收藏+]

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” 博客,谢绝转载!

java-插补查找法,布布扣,bubuko.com

java-插补查找法

原文:http://7832308.blog.51cto.com/7822308/1377107

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!