Exponential search is a search algorithm that is used to find an element in a sorted array by repeatedly doubling the search range until the element is found or the search range exceeds the size of the array.
The algorithm works as follows:
- Start with a range that covers the entire array.
- Check the middle element of the range.
- If the middle element is equal to the target element, return its index.
- If the middle element is greater than the target element, search in the left half of the range.
- If the middle element is smaller than the target element, double the size of the range and continue the search in the new range.
- Repeat steps 2-5 until the element is found or the search range exceeds the size of the array.
Kotlin
fun exponentialSearch(arr: IntArray, target: Int): Int {
if (arr.isEmpty()) return -1 // empty array
// Find the range for binary search by doubling the index until it is greater than or equal to the target
var i = 1
while (i < arr.size && arr[i] <= target) {
i *= 2
}
// Perform binary search within the found range
var left = i / 2
var right = minOf(i, arr.size - 1)
while (left <= right) {
val mid = (left + right) / 2
when {
arr[mid] == target -> return mid
arr[mid] < target -> left = mid + 1
else -> right = mid - 1
}
}
return -1 // target not found
}
how the exponentialSearch
function works:
- Check if the input array is empty. If so, return -1 as the target element cannot be found in an empty array.
- Initialize a variable
i
to 1, which will be used to find the range for binary search. - Use a while loop to keep doubling
i
until it is greater than or equal to the target element or until it reaches the end of the array. This is the exponential search step. The loop conditioni < arr.size && arr[i] <= target
checks that the current index is within the bounds of the array and that the value at that index is less than or equal to the target. - Once the range for binary search has been determined, initialize variables
left
andright
to the first and last indices of the range, respectively. Theleft
index is set toi / 2
becausei
is currently greater than the target element, so the range for binary search should start fromi / 2
. - Perform binary search within the range by using another while loop. The loop condition
left <= right
checks that there are still elements in the search range. - Find the middle index of the search range by computing
(left + right) / 2
. - Compare the value at the middle index with the target element. If they are equal, return the index of the middle element.
- If the middle element is less than the target, update the
left
index tomid + 1
, since the target element must be in the right half of the search range. - If the middle element is greater than the target, update the
right
index tomid - 1
, since the target element must be in the left half of the search range. - If the target element is not found in the search range, return -1.
Exponential search has a time complexity of O(log n), where n is the size of the array. It is particularly useful when the size of the array is unknown, as it avoids the need to determine the size before searching. However, it requires the array to be sorted, and is typically slower than binary search for small arrays.
Kotlin
fun main() {
val arr = intArrayOf(20,30,40,50,60,70,80,90)
val index = exponentialSearch(arr, 30)
if (index != -1) {
println("Target found at index $index")
}
else {
println("Target not found in the array")
}
}
fun exponentialSearch(arr: IntArray, target: Int): Int {
if (arr.isEmpty()) return -1 // empty array
// Find the range for binary search by doubling the index until it is greater than or equal to the target
var i = 1
while (i < arr.size && arr[i] <= target) {
i *= 2
}
// Perform binary search within the found range
var left = i / 2
var right = minOf(i, arr.size - 1)
while (left <= right) {
val mid = (left + right) / 2
when {
arr[mid] == target -> return mid
arr[mid] < target -> left = mid + 1
else -> right = mid - 1
}
}
return -1 // target not found
}