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.
place
letters above
comparisons
jump
1
coc
1
c: 2
3
coa
3, a match
a: 3
coa tried at 2 places of cocoa. Place 2 is jumped over.
if you chose another
BThat is brute force: it tries all 3 places and compares from the front, 3 + 1 + 3.
CThat counts place 3's matches but not place 1's comparison.
DThat tries place 2 as well, which c's jump of 2 skips.
2question 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
AFrom 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.
Bn differs from the pattern's last a at once: 1 a place. n jumps 1, so all 7 places are tried: 7.
Cb is not in the table: 1 comparison, then a jump of 3. Places 1, 4 and 7: 3.
3question 3
A text has 600 letters and a pattern 300. At most how many comparisons can Horspool make, whatever the letters?
the answer, and why
the answer is B
No place compares more than the pattern's 300 letters, and every jump is at least 1. So at most 600 - 300 + 1, which is 301, places are tried: at most 300 times 301, which is 90,300. A text and a pattern of all a's make exactly that many.
CThat compares 300 letters at each of 600 places, but the pattern fits at only 301.
DThat is the fewest, a pattern of b's in a text of a's.
4question 4
Horspool looks for bbbbb in a text of a's. The text grows from 1,000 letters to 2,000. About how many times as many comparisons?
the answer, and why
the answer is A
A place is tried for each 5 letters, as question 3 of the learn page's section 4 finds, so 200 comparisons, then 400. Twice the letters, twice the comparisons.
BThat is how the worst grows, as the text's length squared.
CThe pattern stays at 5 letters, but the text holds twice as many places.
DThat is the jump's length, not how the count grows.
pen and paper
5question 5about 6 minutes
A text holds the letters cabbage. Search it for bag with Horspool, numbering places from 1.
Write the table of jumps.
Which places are tried, and how many comparisons at each?
At which place is bag found, and how many comparisons in all?
a hint
Compare from the pattern's last letter back, then jump by the text's letter above that last letter.
a worked answer
The table: b jumps 2, a 1, any other letter 3. Place 1, cab: g is not b, 1, and b jumps 2. Place 3, bba: g is not a, 1, and a jumps 1. Place 4, bag: g, a and b match, 3, and g jumps 3, past the end. bag is found at place 4, and the comparisons are 1 + 1 + 3, which is 5.
place
letters above
comparisons
jump
1
cab
1
b: 2
3
bba
1
a: 1
4
bag
3, a match
g: 3
bag tried at 3 places of cabbage. Places 2 and 5 are jumped over.
A text holds 801 letters, all a. The pattern has half as many, rounded up.
How many letters has the pattern, and at how many places is it tried if it is all a?
How many comparisons then?
How many if it is all b?
a hint
The learn page's section 4 counts both: question 2 for all a, question 1 for all b.
a worked answer
Half of 801 rounded up is 401 letters. All a: a jumps 1, so it is tried at every place from 1 to 801 - 401 + 1, which is 401. Each place compares all 401 letters, so 401 times 401, which is 160,801. All b: 1 comparison at place 1; a jump of 401 lands on place 402, past the last place, 401. So 1 in all.
Question 2: how many comparisons, , on a text of a's and a pattern of a's, half of rounded up?12 of 12 shown
1the move: a place's most
the line:
why it is allowed: At a place the pattern's letters are compared from its last back, one each, until one differs. It has letters, so at most .
2the move: every letter matches
the line:
place
text
pattern
comparisons
1
aaa
aaa
3, a match
2
aaa
aaa
3, a match
3
aaa
aaa
3, a match
4
aaa
aaa
3, a match
all
12
A text of 6 a's and the pattern aaa. At each of the 4 places all 3 letters match. 4 times 3 is 12.
why it is allowed: The text and the pattern are all a, so every letter matches from the end back: line 1's most, , at every place tried, each a match.
guess it, then
3the move: a jumps 1
the line:
letter
a
any other
jump
1
3
The table for aaa. The nearest a before the last stands 1 from the end; any other letter jumps 3.
why it is allowed: The text's letter above the pattern's last is always a. The nearest a before the last stands 1 from the end, so the letter a jumps 1. For of 1 or 2 the pattern is one a, with no letter before its last, so it jumps its whole length, 1 again. The pattern moves on one place at a time.
guess it, then
4the move: the places
the line:
why it is allowed: By line 3, every place from 1 to the last, where the pattern's letters lie under letters to . For 6 letters and 3, places 1 to 4.
guess it, then
5the move: the pattern half the text
the line:
why it is allowed: The case takes as half of rounded up. Half of rounded up and half rounded down add up to , so is half rounded down, and is 1 more. For 6, 3 and 4.
guess it, then
6the move: multiply by the places
the line:
why it is allowed: Line 2's at each place, times line 4's places, both written in by line 5. For 6, 3 times 4, which is 12; for 7, 4 times 4, which is 16.
guess it, then
7the move: no search costs more
the line:
why it is allowed: By line 1 no place costs more than , and every jump is at least 1. So no search tries more than line 4's places: at most line 6's . For 6, 12.
guess it, then
8the move: a ceiling
the line:
why it is allowed: For of 1 or more: is at most , and is whole, so is at most . is a whole number below , so is at most . Multiply the two. For 6, 12 is at most 36.
guess it, then
9the move: read the bound
the line:
why it is allowed: By line 8, with the fixed multiple 1.
guess it, then
10the move: a floor
the line:
why it is allowed: Rounding up never makes a number smaller, so is at least . Rounding down takes off less than 1, so is more than . Multiply the two, as times is . For 6, 12 against 9.
guess it, then
11the move: read the bound
the line:
why it is allowed: By line 10, with the fixed multiple a quarter.
guess it, then
12the move: both together
the line:
why it is allowed: Lines 9 and 11 bound by the same growth.
guess it, then
try it with a small : at , line 6 gives , which you can work out by hand