public class Halfsearch { public static int halfsearch(int[] array,int key){ int min=0,max=array.length-1,mid; mid=(max+min)/2; while(min<=max){ mid=(max+min)/2; if(key>array[mid]) min=mid+1; else if(key<array[mid]) max=mid-1; else return mid; } return -1; } public static void main(String args[]){ int[] array={2,5,6,8,9,14,89,92,100,125,896}; int index=halfsearch(array,100); System.out.println("index="+index); } }
中级程序员
by: 汉代风云 发表于:2013-07-19 09:29:02 顶(1) | 踩(0) 回复
还不错啊,值得学习学习啊!!
回复评论