Posted on 2008-10-10 16:19
追风舞者 阅读(197)
评论(0) 编辑 收藏 所属分类:
数据结构与算法
public class Search {
/**
* 前提条件array数组已排序
*/
public static boolean binarySearch(int[] array, int target) {
boolean result = false;
int bottom = 0;
int top = array.length-1;
while (bottom <= top) {
int mid = (top + bottom) / 2;
if (target == array[mid]) {
result = true;
break;
} else if (target < array[mid]) {
top = mid - 1;
} else if (target > array[mid]) {
bottom = mid + 1;
}
}
return result;
}
public static void main(String[] args) {
int [] array = {1,3,5,7,9,10};
boolean result = binarySearch(array, 10);
System.out.println(result);
}
}