Sorting: Boyer Moore
Date: 2013-11-25 |
**Big O: **O(nm)
Start by creating a pattern array containing each unique character that appears in the pattern. Next giver each of these elements a weight congruent to the first instance of that character in the pattern, starting from the end of the pattern. These weights will be used in the instance of a negative match between the pattern and provided text.
**Example: **Given a pattern: “kadabra”, the pattern array would look like
k | 6 |
a | 1 |
d | 4 |
b | 2 |
r | 1 |
Start matching the pattern to the text, starting with the last element in the pattern. If the last element matches, then continue on down the pattern until you either reach the end of the pattern or hit a negative match. In the case of a negative match, move the pattern down the text string by spaces equal to that character’s weight. If the text character being matched is not within the given alphabet, move the pattern down by the pattern’s length.
Want more like this?
The best / easiest way to support my work is by subscribing for future updates and sharing with your network.