Gallop search is for searching for an element in a sorted list. You start taking an element at index 0, then at index 1, 2, 4, 8, 16, etc. until you overshoot your target, then you search again in the range that you just found.
What's the time complexity of this? It seems to me to be some sort of logarithmic time complexity, but I can't figure out what.
(Please see my EDIT below)
lets look at the worst-case behaviour. search continues from 0, 1, 2, 4, 8.... lets say n is 2^k for some k >= 0. in the worst-case we might end up searching until 2^k and realise we overshot the target. Now we know that target can be in 2^(k - 1) and 2^k. The number of elements in that range are 2^(k - 1) (think a second.). The number of elements that we have examined so far is O(k) which is O(logn). The following recurrence summarises it.
T(n) = T(n/2) + O(logn)
= T(n/4) + c1log(n/2) + c2logn ((all logs are base 2.))
.
.
.
.
= O((logn)^2)
So the worst-case complexity of this algorithm is quadratic of logn. It maybe not the tightest bound but it is a good upper bound.
EDIT: I'm mistaken. I have taken the definition of gallop search given here literally without following the link. The link says, once we overshoot, we do binary search in the previous interval. It takes log(n) time to overshoot the target and log(n) time to do binary search in the sorted interval. This makes it O(log(n)) algorithm. Thanks to Sumudu Fernando for pointing it out in the comments. I appreciate it.
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