Java集合中二分查找算法实现
Arrays.binarySearch实现了对有序数组特定区间的二分查找,虽然我们觉得很简单,但是阅读源码的确能看到实现这些库的优秀技巧,总是在追求完美和高效。
值得学习的地方有:
(1)边界检查;
(2)求中位数的时候使用位移操作,而不是 x/2;
(3)如果查找的元素不在数组中,通过返回值昭示了应该插入的位置,而不是直接返回-1;
public static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}
// Like public version, but without range checks.
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex – 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid – 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
同样在Collections里面同样有类似的辅助函数,不过是利用迭代器取得特定位置的元素。
public static <T>
int binarySearch(List<? extends Comparable<? super T>> list, T key) {
if (list instanceof RandomAccess || list.size()< BINARYSEARCH_THRESHOLD)
return Collections. indexedBinarySearch(list, key);
else
return Collections. iteratorBinarySearch(list, key);
}
private static <T>
int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key ) {
int low = 0;
int high = list.size()-1;
while (low <= high) {
int mid = ( low + high) >>> 1;
Comparable<? super T> midVal = list.get( mid);
int cmp = midVal.compareTo( key);
if ( cmp < 0)
low = mid + 1;
else if ( cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}
private static <T>
int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key )
{
int low = 0;
int high = list.size()-1;
ListIterator<? extends Comparable<? super T>> i = list.listIterator();
while (low <= high) {
int mid = ( low + high) >>> 1;
Comparable<? super T> midVal = get(i , mid );
int cmp = midVal.compareTo( key);
if ( cmp < 0)
low = mid + 1;
else if ( cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}
/**
* Gets the ith element from the given list by repositioning the specified
* list listIterator.
*/
private static <T> T get(ListIterator<? extends T> i, int index) {
T obj = null;
int pos = i.nextIndex();
if (pos <= index) {
do {
obj = i.next();//链表移动
} while ( pos++ < index);
} else {
do {
obj = i.previous();
} while (-- pos > index);
}
return obj;
}