Skip to content

Boyer-Moore-Horspool

words

Level 2 of 3

Also called Horspool's algorithm and Boyer-Moore-Horspool algorithm.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from boyer-moore-horspool's own run on the numbers shown.

choose one

question 1

Horspool looks for coa in cocoa. How many comparisons does it make?

the answer, and why

the answer is A

The table: c jumps 2, o 1, any other letter 3. Place 1: the text's c is not the pattern's a, 1, and c jumps 2. Place 3: a, o and c match, 3. a stands only last in coa, so it is not in the table: it jumps 3, past the end. That is 4.

placeletters abovecomparisonsjump
1coc1c: 2
3coa3, a matcha: 3
coa tried at 2 places of cocoa. Place 2 is jumped over.

if you chose another

  • That is brute force: it tries all 3 places and compares from the front, 3 + 1 + 3.
  • That counts place 3's matches but not place 1's comparison.
  • That tries place 2 as well, which c's jump of 2 skips.

new numbers

question 2

Each search looks for ana in a text of 9 letters. Which makes Horspool compare the most?

the answer, and why

the answer is D

The table: a jumps 2, n 1, any other letter 3. In anananana, ana matches at places 1, 3, 5 and 7, 3 comparisons each, and a jumps 2 each time: 12.

if you chose another

  • From the end back, a matches, then n differs from a: 2 a place. a jumps 2, so places 1, 3, 5 and 7 are tried: 8.
  • n differs from the pattern's last a at once: 1 a place. n jumps 1, so all 7 places are tried: 7.
  • b is not in the table: 1 comparison, then a jump of 3. Places 1, 4 and 7: 3.

new numbers