Skip to main content

Section 3.16 Search Algorithms (2026)

45 minutes
Search algorithms are an important area of study in computer science. Just think of how often you search for information on the Internet using Google or some other search engine. It’s remarkable how much information Google’s algorithms search through and how fast they deliver the results.
As the video above describes, when you do a Google search, you aren’t actually searching the Web, you’re searching Google’s index of the Web. Google’s spider programs are constantly traversing the web, collecting millions of web pages and organizing them into an index. When you do a Web search Google’s algorithms are searching that index.
What’s the best algorithm for searching an index? An index is an ordered collection. Think of the index that comes at the back of a textbook. It is organized in alphabetical order. Each entry in the index refers to some page in the book.

Subsection 3.16.1 Guessing Game

To help you think about the problem of searching an index we’re going to play a guessing game. The objective of the game is to come up with the most efficient algorithm for guessing a number between 1 and 100, where most efficient means that it takes the fewest number of guesses.
To play the game you can use the widget below (or open in a new window):
One way to look at this game is that we are searching for a number in a list of numbers. Our search made use of the fact that numbers are ordered. The feedback we received - "too high" or "too low" - was based on that order. If you’re still working on figuring out an efficient algorithm, maybe the following widget will give you some ideas. Try to observe the algorithm that the widget is using. (open in a new window)

Activity 3.16.1.

Define a pseudocode algorithm that will efficiently play the guessing game. (Remember that pseudocode is a way of describing an algorithm wihtout using a specific programming language. Your pseudocode should be clear enough that someone else could implement it in a programming language of their choice.)

Subsection 3.16.2 Binary Search Algorithm

There is an efficient algorithm for the guessing game problem, known as the binary search algorithm. It is called binary search because you repeatedly divide the search space into two and eliminate one half of the search space.
Binary Search Algorithm Pseudocode:
The algorithm must have the clause "until you run out of numbers in the list" because the target number may not be in the list. If you run out of numbers, then the target number is not in the list.
For a list of 16 items, you need a maximum of 5 guesses to find the secret number, because \(2^5\) > 16. For a list of 64 numbers you need a maximum of 7 guesses, since \(2^7\) > 64. For a list of 50 numbers, you need a maximum of 6 guesses, since \(2^6\) > 50.
To find the maximum number of guesses needed to find a number in a nonempty list of N items, you need to find the smallest integer k such that \(2^k\) > N. The maximum number of guesses is k.
Do you need a calculator to figure that out? Not really. Here’s an easy way to figure out that maximum number of guesses for any value of N. Just write down the powers of 2 in a table like this:
Table 3.16.1. Powers of 2
x \(2^x\)
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
... ...
So, when N = 100, find the first number in the right column that’s greater than 100 and the number in the left column will tell you how many guesses you need.

Activity 3.16.2.

To guess a number between 1 and 100 using a binary search algorithm, what is the maximum number of guesses the algorithm would take?
  • 100
  • 100 is the worst-case number of guesses when you guess each number in order.
  • 50
  • 50 is the average number of steps when you guess each number in order.
  • 7
  • \(2^7 = 128\text{,}\) so 7 guesses can search at most 127 numbers. This is enough to search a range of 100 numbers.
  • 6
  • \(2^6 = 64\text{,}\) so 6 guesses can search at most 63 numbers. Because 100 is greater than 63, 6 guesses are not always enough for the worst-case scenario.

Activity 3.16.3.

To guess a number between 1 and 500 using a binary search algorithm, what is the maximum number of guesses the algorithm would take?
  • 8
  • \(2^8 = 256\text{,}\) so 8 guesses can search at most 255 items. With 500 items, 8 guesses are not enough to guarantee finding the target.
  • 9
  • \(2^9 = 512\text{,}\) which is greater than 500. Binary search works by dividing the search interval in half with every single guess. The worst-case scenario requires finding the minimum number of steps (k) such that \(2^k\) > N, where N is the size of the range (500).
  • 250
  • This represents the average number of guesses for a linear search (sequential search), where items are checked one by one from beginning to end (N / 2).
  • 500
  • This represents the maximum (worst-case) number of guesses for a linear search, where the target number happens to be the last number checked (N).

Subsection 3.16.5 Search Algorithms Review

In this section, we have learned about two different search algorithms: linear (sequential) search and binary search. Linear search is simpler to implement but slower, while binary search is more complex but much faster, provided the data is sorted.

Activity 3.16.4 Search Algorithms Vocabulary.

Activity 3.16.5.

For searching an unordered list, which search algorithm is the better choice?
  • binary search
  • Sorry, a binary search is only appropriate when the collection you are searching is ordered.
  • linear search
  • That’s right! For searching an unordered list the linear search algorithm is the better choice.

Activity 3.16.6.

For searching a sorted list, which search algorithm is the better choice?
  • binary search
  • That’s right! For searching a sorted list the binary search algorithm is a much more efficient algorithm.
  • linear search
  • Linear search would work, but it would be very slow. There’s a better answer.

Activity 3.16.7.

For which of the problems would the binary search algorithm be useful? Choose all that apply.
  • Looking up a person’s name in the phone book given the person’s phone number.
  • Let me add new information to help you solve this question. A phone book is arranged in order, but it is in order by last name . In order to solve this problem using a binary search, the phone book would need to be in order by phone number.
  • Looking up a word in a Webster’s dictionary.
  • True. A dictionary is arranged in order alphabetically. Thus, a binary search can be used to find any word in a dictionary.
  • Looking up a phone number in the phone book given the person’s full (unique) name.
  • True. A phone book is arranged in order by last name. If you know the person’s full name this includes their last name and you can then perform a binary search to find their phone number.
  • Finding the smallest number in a list of numbers arranged randomly.
  • Let me add new information to help you solve this. A binary search is only appropriate when the collection you are searching is arranged in order .
  • Arranging a deck of cards from the lowest to the highest value cards.
  • Let me add new information to help you solve this question. When you arrange items or objects you are sorting through them. Therefore, a search algorithm is not appropriate for this problem.

Activity 3.16.8.

For which of the problems would the linear search algorithm be useful? Choose all that apply.
  • Looking up a person’s name in the phone book given the person’s phone number.
  • True. A phone book is arranged in order by last name, not by phone number. Therefore, you would need to start at one end of the phone book and check each phone number individually, in order, until you find the phone number you were given and then you can find the last name associated with the phone number.
  • Looking up a word in a Webster’s dictionary.
  • True. A linear search can be used to look up a word in the dictionary. However, a linear search would not be the most efficient search algorithm to use. Since a dictionary is in alphabetical order, you could solve this problem more efficiently using a binary search.
  • Looking up a phone number in the phone book given the person’s full (unique) name.
  • True. A linear search can be used to look up someone’s phone number in the phone book. However, a sequential search would not be the most efficient search algorithm to use. Since the phone book is arranged in order by last name, you could solve this problem more efficiently using a binary search.
  • Guessing a secret number between 1 and 100.
  • True. A linear search can be used to guess a secret number between 1 and 100. However, a linear search would not be the most efficient search algorithm to use. Since the numbers 1 to 100 are ordered numerically, you could solve this problem more efficiently using a binary search.
  • Arranging a deck of cards from the lowest to the highest value cards.
  • When you arrange a collection you are sorting. Therefore, a search algorithm cannot be used to solve this problem.

Activity 3.16.9.

A sorted list of numbers contains 500 elements. Which of the following is closest to the maximum number of list elements that will be examined when performing a binary search for a value in the list?
  • 500
  • This is the number of elements that might be examined in a linear search.
  • 10
  • \(2^9 = 512\text{,}\) which is greater than or equal to to 500.
  • 50
  • 250

Activity 3.16.10.

Suppose you have a deck of cards and you want to find the Ace of Spades. If the deck is shuffled, which is the best search algorithm to use and why?

Activity 3.16.11.

Give an example of a search problem you encounter in everyday life. Does it use sequential, binary, or some other search algorithm?
You have attempted of activities on this page.