15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

424<br />

Sequential Text Search<br />

Sequential Text Search<br />

The simplest text searching algorithm is the brute <strong>for</strong>ce sequential search,<br />

which simply examines every character. To look <strong>for</strong> the string gag in text, the<br />

brute <strong>for</strong>ce sequential search examines the text character by character. The<br />

moment it finds the letter g, it checks to see whether the next letter is a and<br />

so on. To find anything, this search algorithm must exhaustively examine<br />

every character, as shown in Figure 3-1.<br />

In searching <strong>for</strong> the string GAG, a brute <strong>for</strong>ce search starts with the first character<br />

and finds a matching G character. Next, it checks whether the next two<br />

characters are an A and a G.<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 />

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

T<br />

GAT (Fail)<br />

G<br />

A T A G A G A<br />

T<br />

Figure 3-1:<br />

Sequential<br />

search<br />

examines<br />

every<br />

character.<br />

Check second character <strong>for</strong> G<br />

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

Check third character <strong>for</strong> G<br />

T

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

Saved successfully!

Ooh no, something went wrong!