I asked this question in an interview: Suppose an infinity array that sorted How will you find an integer in this array? What will be the complexity of time? I have been guessed that the Interviewer by infinity means that we do not know the value of 'N', where N is the largest number index in the array. I have the following answer: This will be the worst case (low and high) time (log x + 1) time, when sorted from number 1 The numbers of start and end results are as a result. After this, binary search is required: Is that right? If true, can it be optimized? This will take you (Log 2 n) tries to find the right area, and then ((log 2 n) - 1) Trying to find However, if "infinite array" does not have any value beyond
SEARCHINF (A, I, X) {// say that we are searching for x if (a (1)> x) {return } If (A (I) == x) {return i; } And {less = I; High = power (2, i); If (a (i)> gt) {binary-search (a, low, high); } And {SEARCHINF (A, High, X); }} // end else} // end SEARCHINF method
O (log {2 ^ (roof (log x)) - 2 ^ (paving (log x)}}) x within that area (since you have already searched for 0 .. N / 2, you only have to search n / 2..n). Therefore, O (log 2 n + log 2 n - 1) = O (log x or x , then you will never know, because you Will always search
Comments
Post a Comment