有人使用Java实现指数搜索吗?我找不到有关该算法的任何信息,也不知道如何实现它。类似于:
* Signature method that must implement exponential search.
* @ Param searchArray integer array in ascending.
* @ Param x integer element to search for.
* @ Return integer containing the position in the array <CODE> searchArray <\ CODE>
* In case the element <CODE> x <\ CODE> be located in this otherwise
* <CODE> Returns NOT_FOUND </ CODE>
public int exponentialSearch (int [] searchArray, int x);
如Wikipedia中所述,指数搜索算法假定列表已排序,并且由两个阶段组成。
((1)确定搜索关键字所在的(2 k-1,2 k)间隔(k> = 1)]
((2)在此间隔内执行binary search
用于整数数组指数搜索的伪代码:
int exponentialSearch(int arr[], int size, int key)
{
if (size == 0) {
return NOT_FOUND;
}
int bound = 1;
while (bound < size && arr[bound] < key) {
bound *= 2;
}
return binarySearch(arr, key, bound/2, min(bound + 1, size));
}
算法的复杂度为O(log i),其中i是数组中搜索关键字的索引。
[我愿意打赌,这是对binary search的要求,而不是指数搜索,其中您的数据已经按照升序排序了。
大致遵循以下步骤: