function binarySearch (arr, x, floor, ceiling) {
const mid = Math.floor((floor + ceiling) / 2)
if (arr[mid] === x) {
return mid
}
if (floor > ceiling) return -1
if (arr[mid] > value) {
return binarySearch(arr, value, floor, mid - 1)
} else {
return binarySearch(arr, value, mid + 1, ceiling)
}
}
function exponentialSearch (arr, length, value) {
if (arr[0] === value) {
return 0
}
let i = 1
while (i < length && arr[i] <= value) {
i = i * 2
}
return binarySearch(arr, value, i / 2, Math.min(i, length))
}
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
const value = 78
const result = exponentialSearch(arr, arr.length, value)
if (result < 0) {
console.log('Element not found')
} else {
console.log('Element found at position :' + result)
}