# 二分查找—Java实现

• 最坏情况下，关键词比较次数为，且期望时间复杂度为O()

``````package Common;

public class Common {
public static boolean less(Comparable a,Comparable b) {
return a.compareTo(b)<0;
}
public static void exch(Comparable[] a,int i,int j) {
Comparable temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}``````
``````/* *1.二分查找只能查找顺序存储的数据 数据是有序的 *2.每次取数组的中间值作为比较对象 * 如果小于a[mid] 则在数组的右边继续比较 * 直到找到或者结束 * 如果大于a[mid] 则在数组的左边继续比较 *3.时间复杂度为O(logn) *4.静态表建议使用二分查找 * 若是动态表 不建议使用 * */
public class BinarySearch {
//非递归实现
public static int search(Comparable[] a,Comparable key) {
int low=0;
int high=a.length-1;
while (low<high) {
int mid=(low+high)/2;
if (Common.less(key, a[mid])) {
high=mid-1;
}
else if (Common.less(a[mid], key)) {
low=mid+1;
}
else {
return mid;
}
}
return 0;
}
//递归实现
public static int search(Comparable[] a,int low,int high,Comparable key) {
while (low<high) {
int mid=(low+high)/2;
if (Common.less(key, a[mid])) {
search(a, low, mid-1, key);
}
else if (Common.less(a[mid], key)) {
search(a, mid+1, high, key);
}
else {
return mid;
}
}
return 0;
}
public static void main(String[] args) {
Integer[] a= {1,3,4,5,6,8};
int low=0;
int high=a.length-1;
int i=search(a, 4);
System.out.println("非递归查找位置在"+(i+1));
System.out.println("===============");
int j=search(a, low, high, 4);
System.out.println("递归查找位置在"+(j+1));
}

}``````