Table of Contents

Boyer-Moore-Horspool is the fastest way to search for substrings. If you are working on a searchbars, an auto-corrector, or Big Data and you are not using this algorithm, you are wasting CPU cycles and your time.

What is Boyer-Moore-Horspool Algorithm ?

Boyer-Moore-Horspool is an algorithm for finding substrings into strings. This algorithm compares each characters of substring to find a word or the same characters into the string. When characters do not match, the search jumps to the next matching position in the pattern by the value indicated in the Bad Match Table.

The Bad Match Table indicates how many jumps should it move from the current position to the next.


The algorithm was published by NIgel Horspool, in 1980, a professor of computer science at the University of Victoria. He is co-inventor of Dynamic Markov Compression and editor-at-large of the journal Software. Also, he is the author of C Programming in the Berkeley UNIX Environment.

His work was based on Boyer–Moore string search algorithm. This is related to the Knuth–Morris–Pratt algorithm. Horspool improve the efficiency of the original algorithm to achieve the complexity to O(mn) in the worst case.

How Does It Works?

As an example, we will find “abcd” into the string “eovadabcdftoy.”

The first step is calculate the value of each letter of the substring to create the Bad Match Table, using this formula,

Value = length of substring - index of each letter in the substring - 1

Note that the value of the last letter and other letters that are not in the substring will be the length of the substring

Finally, the value should be assigned to each letter in the Bad Match Table. After calculating the value, your table will look like this,

After that, you can compare the substring and the string. You start from the index of the end letter in the substring, in this case the letter "d."

  • If the letter matches, then compare with the preceding letter, "c" in this case.
  • If it doesn't match, check its value in the Bad Match Table.
  • Then, skip the number of spaces that the table value indicates.

Repeat this steps until all the letters match.

Here's another example,


This code implements the Boyer-Moore-Horspool algorithm in Python.

<script src=""></script>



The Boyer-Moore-Horspool algorithm execution time is linear in the size of the string being searched. It can have a lower execution time factor than many other search algorithms.

For one, it does not need to check all characters of the string. It skips over some of them with help of the Bad Match table.

The algorithm gets faster as the substring being searched for becomes longer. This is because with each unsuccessful attempt to find a match between the substring and the string, the algorithm uses the Bad Match table to rule out positions where the substring cannot match.


In the worst-case the performance of the Boyer-Moore-Horspool algorithm is O(mn), where m is the length of the substring and n is the length of the string.

The average time is O(n). In the best case, the performance is sub-linear, and is, in fact, identical to Boyer-Moore original implementation.

Regardless, the Boyer-Moore-Horspool is quicker and the internal loop is simpler than Boyer-Moore.


Boyer-Moore-Horspool is faster, simpler and optimized the searches of substrings. It has the following uses,

  • Searchbars
  • Auto-correctors
  • String Analyzers
  • Big Data
  • Text labeling

Boyer-Moore-Horspool is the best algorithm for string searches.


Learn More about Encora

We are the software development company fiercely committed and uniquely equipped to enable companies to do what they can’t do now.

Learn More

Global Delivery






Related Insights

Mastering Microsoft Microsoft Azure Migration: A Comprehensive Guide

Learn about Azure Migrate, the Azure migration process, tools, and services with our expert guide. ...

Read More

Harnessing RAG: LangChain vs. Pandas for Superior Conversational Analytics

This blog explores the differences between two popular tools used for this task, LangChain, and ...

Read More

Future-proofing Skills: Thriving in the Transforming Energy & Utilities industry

Encora has developed video-based and game-based microlearning series for topics such as contingency ...

Read More
Previous Previous

Accelerate Your Path
to Market Leadership 

Encora logo

+1 (480) 991 3635

Innovation Acceleration

Encora logo

+1 (480) 991 3635

Innovation Acceleration