|
The whole problem occurs when we get a mismatch. Otherwise the algorithm is just a regular sequential search. So, lets assume you started the search at index a and it failed at some indexb. Now, we have 2 choices:
1. Restart the search from index b + 1. 2. Restart the search from index k, where k is a < k <= b.
I assume it's obvious why these are the only 2 choices we have - if the word started somewhere at index i < a it would've been found already.
Now, how do we make a choice between those two? Well, if between a and b there is no letter that matches the first letter of our word, then there is no way in hell that we will get a match at that range. So we choose the first choice. Otherwise, if there is at least one such letter, we restart the search at its first occurrence.
I think it's pretty simple. Check out Wikipedia's article about it. Maybe it will be a bit clearer.
|