My first question in the analysis it is mentioned as
n+(n/2)+(n/4)+— is atmost 2n. how we got result as atmost 2n?
We have a collection of arrays, where array “i” has size (2 to power
i). Each array is either empty of full, and each is sorted. However,
there will be no relationship between the items in different arrays.
The issue of which arrays are full and which is empty is based on the
binary representation of the number of items we are storing.To perform a lookup, we just do binary search in eac occupied aray. In
the worst case this takes time O(log(n) + log(n/2) + log(n/4) +….+1)
= O(log square n).Following is question on above text snippet.
- How does author came with O(log(n) + log(n/2) + log(n/4) +….+1) ?
- and above sum is O(log square n).
Thanks!
n + n/2 + n/4 + n/8 … = n * (1/1 + 1/2 + 1/4 + 1/8 + …)
The sum 1/1 + 1/2 + 1/4 + 1/8 + … is a geometric series that converges to 2, so the result is 2n.
Apparently, the author is talking about a collection of arrays with the sizes n, n/2, n/4, …, and he is doing a binary search in each of them. A binary search in an array with n elements takes O(log n) time, so the total time required is O(log n + log n/2 + log n/4 + …).