Binary Search on Bits

Computer Science Level pending

Chris realize his computer couldn't perform accurate comparisons on integers. That is, his computer couldn't evaluate if a > b is true or false.

He has a sorted array of n n integers and he needs to find if the number x x is within the array. He doesn't want to perform a linear search, so he came up with another idea of Binary Search. Here is the algorithm :

  1. Look at the k = 0 k=0 bit of x x , it's either 1 or 0.
  2. If it's 1, binary search for the lower bound that has the k k -th bit as 1.
  3. If it's 0, binary search for the upper bound that has the k k -th bit as 0.
  4. Increment k k by 1 and go back to step 1.

What is the time complexity of this algorithm? Or if you think this algorithm doesn't work, choose the option "This algorithm doesn't work".

Details and Assumptions

  • Notice that this algorithm doesn't use any of the following operators : < > <= >= .
  • This algorithm is written in Python, where there is no upper bound for x x like C++.
O ( n lg x ) O(n\lg x) This algorithm doesn't work. O ( x lg n ) O(x\lg n) O ( lg x ) O(\lg x) O ( lg n ) O(\lg n) O ( lg 2 n ) O(\lg ^2 n) O ( lg n lg x ) O(\lg n \lg x)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

0 solutions

No explanations have been posted yet. Check back later!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...