liveBook · Manning

Algorithm

As explained in How Computers Make Books

An algorithm is a finite, explicit sequence of instructions for carrying out a computation or solving a problem.

To find a word in a line of text, “just look for it” is not enough. A computer needs each action made explicit: compare characters, decide whether they match, move one place right, and know when to stop. Such a careful method, assembled from small steps with no unstated assumptions, is an algorithm. section 4.1

Consider searching for horses in “houses and horses and hearses.” Number the characters from 0. Place the word at the first text position and compare corresponding characters. A single difference rejects that position; then shift the word right by one and try again. The match begins at position 11. Once the word would run beyond the text’s end, stop: no later position can match. section 4.1

An algorithm can first be described in ordinary language and diagrams, then made precise as a program. Pseudocode keeps the focus on the method rather than a particular programming language. The search procedure checks that enough text remains, prints a matching position, and calls itself with the next position. A separate comparison procedure either advances through matching characters or returns failure at the first mismatch. section 4.2

Listing 4.9 The whole program
1 define compare pt pw 
2   if pw = length W then return true 
3   if T[pt] = W[pw] then compare (pt + 1) (pw + 1) 
4   return false 
5 
6 define search pt 
7   if pt + length W <= length T then 
8     if compare pt 0 then print pt 
9     search (pt + 1)

This complete pseudocode listing separates comparing characters from advancing the search position, making the stopping conditions visible. section 4.2

Algorithms are built by reasoned steps and can be tested by tracing them manually on paper. Correctness matters first: the method must find every occurrence without reading past the end. Efficiency matters too, because large data sets make repeated small operations costly. section 4.6

How How Computers Make Books covers it

How deep it goes
Works through a complete pseudocode search and comparison procedure, including bounds, matching, failure, and recursive advancement.
Examples
Traces horses through “houses and horses and hearses,” locating the match at position 11.
What it assumes
Starts with character equality and teaches the needed indexing, bounds checks, and pseudocode notation from that basis.
What you can do after
Build it from scratch as a pseudocode text search that tests each possible starting position.
How it connects
Connects algorithms with text search, character comparison, pseudocode, parameters, correctness, and efficiency.

Also explained in

Compare how 2 books explain algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Computers Make Books. The section links point to the parts of the book it draws on.