15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sequential Text Search 425<br />

In this example, the third character (T) doesn’t match, so the brute <strong>for</strong>ce algorithm<br />

starts all over again by examining the second character, even though it<br />

had previously examined that character. Because the brute <strong>for</strong>ce method<br />

examines every character individually, this method is the slowest and least<br />

efficient method <strong>for</strong> finding text.<br />

Although the brute <strong>for</strong>ce method works, it can take too much time, especially<br />

when searching through large amounts of text. To make text searching<br />

faster and more efficient, computer scientists have developed a variety of<br />

alternative algorithms.<br />

The Boyer-Moore algorithm<br />

To speed up text searching, the computer should skip any previously examined<br />

text and jump straight to unexamined text. That’s the basis <strong>for</strong> a text<br />

searching algorithm developed by two computer scientists (Bob Boyer and<br />

J. Strother Moore) called the Boyer-Moore algorithm.<br />

Like the brute <strong>for</strong>ce algorithm, the Boyer-Moore algorithm examines text<br />

character by character. After the Boyer-Moore algorithm finds a partial<br />

match, it’s smart enough to skip over previously examined characters and<br />

start searching from the end of all examined characters, speeding up the<br />

entire search process, as shown in Figure 3-2.<br />

Searching <strong>for</strong> the string GAG<br />

G<br />

A T A G<br />

A<br />

G<br />

A<br />

T<br />

G<br />

G A T A G A G A<br />

T<br />

GA<br />

Book IV<br />

Chapter 3<br />

Figure 3-2:<br />

The Boyer-<br />

Moore<br />

algorithm<br />

skips over<br />

partially<br />

matched<br />

characters.<br />

G A T A G A G A<br />

G<br />

GAT (Fail)<br />

A T A G A G A<br />

Skip previously examined text<br />

T<br />

T<br />

String Searching

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!