Searching+Algorithms

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.

Because indexes are ordered, searching them can be done very efficiently. To illustrate this, let's look at an analogous problem – that of guessing a secret number.

Guessing Game: I'll Guess Your Secret Number
TRY OUT GUESSING GAME

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. The algorithm used in this game is known as //**binary search**// – so called because it repeatedly divides the search space into two parts. It keeps the half that possibly contains the secret number and throws away or ignores the other half. Binary search is an example of a **divide and conquer** approach to solving the problem, so-called, because it breaks the big problem into smaller problems and works on the smaller problems. Here's a summary of the algorithm in pseudocode:

Pseudocode for Binary Search for a Target Number in a List of Numbers
Note that this algorithm accounts for the possibility that the Target number may not be in the list, which wasn't the case in our Guessing Game.
 * Repeat until your guess is correct or until you run out of numbers in the list.
 * Guess that the target number is the middle number in the list.
 * If the guess is too high, Cut off the top half of the list.
 * If the guess is too low, Cut off the bottom half of the list.
 * If the guess is correct, Stop. ||

Now You Guess My Secret Number
Can you apply the //**binary search**// algorithm? In this version of the game the computer will pick a secret number and you will try to guess it. If you understand the binary search algorithm, you should be able to guess a secret number between 1 and 100 in at most 7 guesses. And you should be able to guess a number between 1 and 1000 in at most 10 guesses. Give it a try!

Guessing Game 2

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.

Sequential Game

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. Here's a summary of the sequential search algorithm. Let's suppose we have 16 boxes numbers 1 to 16, each containing a letter, but that the words are not in any particular order: So in this algorithm we are letting //b// keep track of what box we are searching. It starts at 1 and increases by 1 so that we will look at every box until we find 'F' or run out of boxes. If we find 'F' we report what box it was in by reporting //b//'s value. If we don't find it, we report that it wasn't found.
 * **Problem: Find the letter 'F'** || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 ||
 * U || E || Z || A || M || L || S || T || B ||  || **Pseudocode of Sequential Search Algorithm**
 * Let **b** represent the box number to search, initially 1
 * Repeat until you find 'F' or run out of boxes to search
 * Look in box //b//. If 'F' is in box //b//, stop and report //b//'s value.
 * Otherwise, add 1 to //b//
 * If you don't find 'F' in any box, report it not found. ||

Searching for 'F' in this set of boxes represents our **//worst case scenario//** because our algorithm would have to look in every box to conclude that 'F' was not in the boxes.

Reflection: For Your Portfolio
Create a page named //**Search Algorithms**// under the //Reflections// category of your portfolio and answer the following questions:
 * 1) 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?
 * 2) What other examples of searching have you seen in the real world? Do they use sequential, binary, or some other search?