新浪的笔试题,二分法查找算法。
package com.gp.common.util;
import java.util.List;
/**
* 二分法查询算法
* @author Administrator
*
*/
public class UtilMethod
{
public static int binary(int[] array, int value)
{
int low = 0;
int high = array.length - 1;
while (low <= high)
{
int middle = (low + high) / 2;
if (value == array[middle])
{
return middle;
}
if (value > array[middle])
{
low = middle + 1;
}
if (value < array[middle])
{
high = middle - 1;
}
}
return -1;
}
public static void main(String[] args)
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int value = binary(a, 9);
System.out.println(value);
}
}