Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search algorithm and its complexity

I was asked this question in an interview: Assume an infinite array of integers which is sorted. How would you search for an integer in this array? What would be time complexity? I guessed what the interviewer meant by infinite is that we dont know the value of 'n', where n is the index of the largest number in the array. I gave the following answer:

SEARCHINF(A,i,x){ // Assume we are searching for x
if (A(1) > x){
   return
}
if(A(i) == x){
   return i;
}
else{
    low = i;
    high = power(2,i);
    if (A(i)>x){
       BINARY-SEARCH(A,low,high);
    }
    else{
        SEARCHINF(A,high,x);
    }
}// end else
}// end SEARCHINF method

This will find the bound(low and high) in (log x + 1) time in the worst case, when the sorted numbers start from 1 and subsequent numbers are consequent. Then the binary search requires:

O( log {2^(ceil(log x)) - 2^(floor(log x))} )

Is this correct? If correct, can this be optimized?

like image 689
Brahadeesh Avatar asked Mar 16 '11 18:03

Brahadeesh


People also ask

What is the complexity of search algorithm?

Complexity Analysis The worst occurs when the algorithm keeps on searching for the target element until the size of the array reduces to 1. Since the number of comparisons required is logn, the time complexity is O(logn). Binary search has an average-case complexity of O(long).

What is the search algorithm?

In computer science, a search algorithm is an algorithm (if more than one, algorithms) designed to solve a search problem. Search algorithms work to retrieve information stored within particular data structure, or calculated in the search space of a problem domain, with either discrete or continuous values.

What is search algorithm with example?

What is a Search Algorithm? This kind of algorithm looks at the problem of re-arranging an array of items in ascending order. The two most classical examples of that is the binary search and the merge sort algorithm.


2 Answers

Using the method of double your index until you pass it, then binary search the region you just jumped over (what it looks like your pseudocode is trying to do), the time spent should be O(log2 n) where n is the index of the item you are searching for.

It will take you (log2 n) tries to find the correct region, and then ((log2 n) - 1) tries to find x within that region (since you already searched from 0..n/2, you only need to search n/2..n). Therefore, O(log2 n + log2 n - 1) = O(log2 n).

However, if the "infinite array" does not contain x or any value greater than x, you will never know, because you will search forever.

like image 149
Jonathan Avatar answered Sep 30 '22 05:09

Jonathan


The sorted array gives it away indeed: binary sort.

Worst case scenario: O(lg(n)). There's no faster, assured way of finding it.

Of course, you could just tell him that finding an element in an infinite array will take forever.

like image 31
Carra Avatar answered Sep 30 '22 07:09

Carra