本帖最后由 乔叶旭 于 2012-11-28 21:23 编辑
class Zhebanchazhao
{
public static void main(String[] args)
{
int[]a = new int[]{2,3,7,4,23,8};//定义一个数组
System.out.println(fun(a,4));
}
public static int fun(int[] arr,int key) //折半查找算法 总提示这句出错,很迷茫。。。。。
{
int a = 0,b = arr.length-1;
int m = (a + b)/2;
while (a<=b)
{
if (arr[m]> key)
{
b = m/2-1;
}
if (arr[m] = key)
{
return m;
}
else {
a = m/2+1;
}
}
return -1;
}
} |