
[30, 20, 47, 63, 101, 115]
[
20, 30,
47, 63, 101, 115]
Step 6
[20, 30, 47, 63, 101, 115]
4.2 Binary search (half-interval search, logarithmic search)
Imagine that we have to write an algorithm that checks if a word exists (or not) in the
dictionary. We can assume that the dictionary has been previously stored in an array whose
elements are of “string” type. Let's say the array (and therefore the dictionary) contains
40,000 elements.
A first way of checking whether a word is in the dictionary consists of successively
examining all the words in the dictionary, from the first one to the last one, and comparing
them with the word to be checked.
It works, but it may be being very time-consuming: If the word is not in the dictionary, the
algorithm will only know it after 40,000 iterations! And even if the word is in the
dictionary, the answer will still require an average of 20,000 iterations.
However, there is another way to perform this search, taking advantage of the fact that in
a dictionary, the words are sorted in alphabetical order.
This is binary search.
Moreover,
a human being looking for a word in the dictionary never reads all the words, from the first
to the last one: He/she uses the fact that the words are sorted.
Binary research consists of comparing the word to be checked with the word that is right
in the middle of the dictionary. If the word to be checked is earlier in the alphabetical order,
we know that we will have to look for it from now on in the first half of the dictionary.
Otherwise, we know we will have to look for it in the second half.
From there, we take the half of the dictionary that we have left, and we start again: we
compare the word to look for with the one that is in the middle of the remaining piece of
the dictionary. We discard the bad half, and we start again, and so on.
By dint of cutting our dictionary in half, then in half again, etc. we will end up with a piece
that only contains one word. And if we have not come across the right word at one time or
another, it is because the word to check is not in the dictionary.
Let's see what this gives in terms of the number of operations to be performed, choosing
the worst case: The one where the word is absent from the dictionary.
Initially, we look for the word among 40,000.
After test n°1, we only look for it among 20,000.
After test n°2, we only look for it among 10,000.
After test n°3, we only look for it among 5,000.
…
After test n°15, we only look for it among 2.
After test n°16, we only look for it among 1.