Suppose, the array is infinite. So, the problem here is that we don’t know the size of the array. If the array is infinite, that means we don’t have proper bounds to apply binary search. So in order to find the position of the key, what can I do.
It will no longer qualify as a pure binary search, but here's one approach.
For an array array
and a search value s
:
n
(e.g. n = 1000
)i
corresponding to the first element in the array (i = 1
)s
to the value at array[n]
array[n]
is greater than s
:array[i]
and array[n]
array[n]
is less than s
:i = i + n
n = n * 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With