/**
* Exponential Search
*
* The algorithm consists of two stages. The first stage determines a
* range in which the search key would reside if it were in the list.
* In the second stage, a binary search is performed on this range.
*
*
*
*/
function binarySearch(arr, value, floor, ceiling) {
// Middle index
const mid = Math.floor((floor + ceiling) / 2)
// If value is at the mid position return this position
if (arr[mid] === value) {
return mid
}
if (floor > ceiling) return -1
// If the middle element is great than the value
// search the left part of the array
if (arr[mid] > value) {
return binarySearch(arr, value, floor, mid - 1)
// If the middle element is lower than the value
// search the right part of the array
} else {
return binarySearch(arr, value, mid + 1, ceiling)
}
}
function exponentialSearch(arr, length, value) {
// If value is the first element of the array return this position
if (arr[0] === value) {
return 0
}
// Find range for binary search
let i = 1
while (i < length && arr[i] <= value) {
i = i * 2
}
// Call binary search for the range found above
return binarySearch(arr, value, i / 2, Math.min(i, length))
}
export { binarySearch, exponentialSearch }
// const arr = [2, 3, 4, 10, 40, 65, 78, 100]
// const value = 78
// const result = exponentialSearch(arr, arr.length, value)
给定一个包含 n 个元素的已排序数组,编写一个函数来搜索给定元素(目标)的索引。
arr = [1, 2, 3, 4, 5, 6, 7, ... 998, 999, 1_000]
target = 998
index = 0
1. SEARCHING FOR THE RANGE
index = 1, 2, 4, 8, 16, 32, 64, ..., 512, ..., 1_024
after 10 iteration we have the index at 1_024 and outside of the array
2. BINARY SEARCH
Now we can apply the binary search on the subarray from 512 and 1_000.
注意:我们从 512 到 1_000 应用二分查找,因为在 i = 2^10 = 1_024
时,数组已结束,并且目标数字小于数组的最新索引(1_000)。
最坏情况: O(log *i*)
,其中 *i* = index
(位置)为目标。
最佳情况: O(*1*)
⌈log(i)⌉
次后,算法将位于大于或等于 i 的搜索索引处。我们可以写 2^⌈log(i)⌉ >= i
2^i - 2^(i-1)
,换句话说,即“(从开始到 i 的数组长度) - (跳过到前一次迭代的部分)”。很容易验证 2^i - 2^(i-1) = 2^(i-1)
经过详细解释后,我们可以说指数搜索的复杂度为
O(log i) + O(log i) = 2O(log i) = O(log i)
让我们用一个不太理论的例子来看看这个比较。假设我们有一个包含 1_000_000
个元素的数组,我们想要搜索位于第 4
个位置的元素。很容易看出