Question: We know we can do binary search on a sorted array A[1::n] in (log n) time. But what if we dont know the value n?
We know we can do binary search on a sorted array A[1::n] in (log n) time. But what if we dont know the value n? Your task in this problem is to give (log n)-time algorithm to search such an array for a target key K, returning the index in A of K, if it exists, or else -1. (You can assume for simplicity that the values of A are all distinct.)
Since you dont know As length, a common occurrence will be falling of the end of A, i.e., trying to access A[i] for an i greater than (the unknown value of) n. In programming doing this might return an error or throw an exception. For the purposes of this problem, it may be simpler to assume that A[i] will evaluate to 1" when i > n, i.e., a comparison between A[n+1] and other actual key (either the target K or any A[k] for 1 k n will always say A[n + 1] is larger.![We know we can do binary search on a sorted array A[1::n]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f462036147f_90666f46202a71ff.jpg)
2. We know we can do binary search on a sorted array A[1.n] in e(log n) time. But what if we don't know the value n? Your task in this problem is to give e(log n)-time algorithm to search such an array for a target key K, returning the index in A of K, if it exists, or else -1. (You can assume for simplicity that the values of A are all distinct.) Since you don't know A's length, a common occurrence will be "falling of the end" of A, ie., trying to access Ali] for an i greater than (the unknown value of) n. In programming doing this might return an error or throw an exception. For the purposes of this problem, it may be simpler to assume that A[i] will evaluate to "oo" when i > n, i.e., a comparison between An +1] and other actual key (either the target K or any Alk] for 1 3 k 3 n will always say Ain +1] is larger
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
