5.3. Search Algorithms¶
Time Estimate: 45 minutes
5.3.1. Introduction and Goals¶
Search is 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.  
Learning Objectives: I will learn to
Language Objectives: I will be able to

5.3.2. Learning Activities¶
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.
POGIL Activity for the Classroom (15 minutes)
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):
Or, you can play the game without a computer, in which case one team member will think of a secret number between 1 and 100 and the other team members will collaborate to try to come up with the best guess. Just as in the widget, after each guess, the person who knows the secret will tell the guessers whether the guess was too high or too low or just right.
After figuring out a good algorithm, write it in pseudocode.
Break into POGIL teams of 4. Record your answers using this worksheet. (FileMake a Copy to have a version you can edit.)
Role  Responsibility 

Facilitator  For each trial of the guessing game, the facilitator records the team's guesses and the result (too high or too low or just right) and keeps track of how many guesses are made. 
Spokesperson  Reports the team's pseudocode algorithm. 
Quality Control  Tests the algorithm, using the widget or by playing the guessing game by hand. 
Process Analyst  Keeps track of the teams progress and assesses its performance and records on the Portfolio the team's answers to the following guided inquiry questions. 
Questions
 (Portfolio) Define a pseudocode algorithm that will efficiently play the guessing game.
 (Portfolio) To guess a number between 1 and 100, what's the maximum number of guesses your algorithm would take?
 (Portfolio) To guess a number between 1 and 500, what's the maximum number of guesses your algorithm would take?
Guessing Game: I'll Guess Your Secret Number
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 widget in new window.)
An Efficient 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. Click here to see the pseudocode or see the algorithm comparison section below.
Linear (or Sequential) Search
What if you had to search a set of data that was not sorted? Binary search won't work in that case. To illustrate this problem, let's try a variation of our guessing game. This time the app will only tell you if your guess is right or wrong, not whether it is too high or too low. Try it. (Open widget in new window.)
As you can see from this game, if you don't know the order of the items you are going to search, you have no choice but to search them sequentially if you definitely want to find the secret number.
Comparing Linear vs. Binary Search Algorithms
Here is a comparison of sequential search and binary search looking for a target in a list of N items in AP style pseudocode. Don't worry about understanding the details about the binary search algorithm, but do understand the general way it works. Binary search is more complex but it is much faster. However, the list must be in a sorted order for a binary search to work. Linear search is slower but works with any list in any order.
Linear Search Pseudocode  Binary Search Pseudocode 

FOR EACH item in List { IF (item = target) DISPLAY "Found target!" } 
low ← 0 high ← N middle ← item (low+high)/2 (compute the middle of the list, rounded down) REPEAT UNTIL (middle = target OR low > high) { IF (target < middle) high ← middle  1 (This cuts off the top half of the list) IF (target > middle) low ← middle + 1 (This cuts off the bottom half of the list) middle ← item (low+high)/2 (compute new middle) } IF (middle = target) DISPLAY "Found target!" ELSE DISPLAY "Target not in list" 
5.3.3. Summary¶
In this lesson, you learned how to:
5.3.4. SelfCheck¶
Vocabulary
Here is a table of the technical terms we've introduced in this lesson. Hover over the terms to review the definitions.
binary search
linear or sequential search 
Check Your Understanding
 Linear search
 That's right! For searching an unordered list the linear search algorithm is the better choice.
 Binary search
 Sorry, a binary search is only appropriate when the collection you are searching is ordered.
Q1: For searching an unordered list, which search algorithm is the better choice?
 Linear search
 Linear search would work, but it would be very slow. There's a better answer.
 Binary search
 That's right! For searching a sorted list the binary search algorithm is a much more efficient algorithm.
Q2: For searching a sorted list, which search algorithm is the better choice?
 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.
 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.
 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 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.
 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 .
Q3: For which of the problems would the binary search algorithm be useful? Choose all that apply.
 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.
 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.
 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 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.
 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.
Q4: For which of the problems could the linear search algorithm be used? Choose all that apply.
 10
 50
 250
 500
Q5: AP 2021 Sample Question: 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?
5.3.5. Reflection: For Your Portfolio¶
Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.