Sunday, September 11, 2016

Binary Search


[ source ]
The Basic and Advanced Strategies which we have been discussing for "Guess The Number" are so powerful in other contexts that we all use them in our daily lives without even thinking about it.

To find a word in a dictionary, you don't start at the beginning and read the whole book. You choose a page near the middle, check what it says, and narrow your search to the front or the rear as appropriate. And you repeat this process as many times as necessary until you find what you're looking for.

We use the same technique when searching a phone book, an encyclopedia, or an index. Any sorted list lends itself to this technique, which is sometimes called "Binary Search."

This is not the same as the "False Binary" which we sometimes see in politics, for instance, where people are told they must be "Democrat or Republican," or "Liberal or Conservative," or "with us or against us," and where no other options are ever acknowledged, even though they do exist.

This approach might be called "True Binary," because no matter where you look, if you don't find the item you're seeking, then it must be either before or after the place where you're looking. It can't be both, and there is never any third option.

When we use Binary Search to find a phone number, for instance, we don't try to look at the middle of the range every time. Looking anywhere near the middle of the range is good enough.

This is because we are not trying to find the number in the fewest possible steps. We are trying to find it as quickly as possible. And we can't pinpoint the middle of the range easily, as we can in "Guess The Number." So we don't bother. We just make one reasonable guess after another because that's faster.

Computer programs use Binary Search in many different ways, some of them far from obvious. And software can find the middle of a sorted list quite easily, so that's what computer programs do. We might even say that computers always use Basic Strategy whereas humans always use Advanced Strategy.

Whether we do it ourselves or program our computers to do it; whether we use Basic or Advanced Strategy, we like Binary Search for its efficiency, especially when we are working with a long list.

In "Guess The Number," Basic Strategy can find any number between 0 and 32 in at most 5 guesses.

And it's called Binary Search because it cuts the range in half each time, so we can find a number between 0 and 64 in 6 guesses, between 0 and 128 in 7 guesses, and so on.

If a list has a thousand items, a binary search takes 10 steps. If it has a million items, the search takes 20 steps. If the list has a billion items, the search takes 30 steps. And so on.

Unless a list is very short, it always makes more sense to use Binary Search than to read the whole thing. And the longer the list, the more sense it makes, as long as the list is sorted.

People say, "finding a needle in a haystack" when they mean, "solving a very difficult problem."

With Binary Search, you can find a needle quickly and easily, provided you can sort the haystack.

Next: Triple Bogus When Lit
Previous: What Causes the Mandela Effect?
Home: Contents
~~~
Your comments are invited.