i have a general question (taken from some computer science test) i’d like to get an explanation for, the question was:
Sorted array given a particular size, and suppose we use the fastest method in computer science to find values in the array. In this method, the search time of any element is no more than N seconds. Now we multiply the size of the array. The search time of any element in the array will be at most:
Answer: N+1.
Can anyone please give me a full explanation why this is the answer? and why isn’t it 2*N ?
Thanks.
I suppose they mean binary search algorithm as fastest method for searching in sorted array. It is algorithm of type “divide and conquer”, so for one step of such algorithm we reduce searching area by half.
Example:
Let’s define one step in our algorithm take 1 unit of time
It is how binary search works and consumes 3 units of time.
Now imagine we doubled array to [1..16], we need 1 more step to reduce array to previous one, so we need 4 units of time.