11 Years
Discussion Span
Last Post by cms271828

One thing to note... Binary search requires an Ordered List,
and the array above is not ordered.

Assuming your arrays are ordered...
You could just use the pseudo-code for it...
Or better is to construct it yourself from scratch.

Start with L=0 (leftest element) R=9 say (Rightest element)
Your first guess is the middle one, so just take

if array[M] is the value you want, you are done.
If array[M] is greater than the one you want, you can squash down the right hand side to restrict your range. So R=M-1.
If array[M] is smaller than the one you want, you can push up the left hand side to restrict your range. So L=M+1.

Then just repeat it, and you will get to your value very very quickly since binary search is exponential, so even if your array had as many elements as atoms in the universe, you would find your answer almost instantly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.