# Sequential and Binary Search

```Sequential and Binary Search
Computer Science I --- Haas
How can we find something in a list?
A linear or sequential search is a method for finding a particular item in a list. It consists
of checking every item, one at a time and in sequence, until the desired one is found.
Linear search is the simplest search algorithm, but it is not very efficient.
A binary search compares the search item to the middle element of the list. If it matches
the item has been found. Otherwise, if the search item is less than the middle element, the
algorithm repeats the search on the first half of the list, if the search item greater the
algorithm repeats the search on the second half of the list. This process continues until the
item is found or it can be determined that the item is not in the list.
One advantage of a linear search is that it can be performed on a list that is unsorted. A
binary search can only be performed on a sorted list.
From your classes folder open: searching - NOT COMPLETE
Once you start making changes to it, save your version as: LASTNAME-searching
Assignment: You will complete two blocks.
Implement using a linear or
sequential search.
Implement using a binary
search.
You will need to show Haas code for both searches.
1) Sequential search: Implement the block: find in unsorted list. The block should accept
a word to search for and a list of words as input, and return the index of the word’s
position in the list. The block should return the location of the word, or -1 if the word
doesn't exist in the list.
See next page for an hint…
Below is the partially compete block to perform a sequential search. It looks for word in
list. Notice that if the word is not found it will repeat until it reaches the end of the list.
Inside the repeat-block you should have an if-block that checks to see if the word is found
and, if found, reports that index.
If you reach the end of the list you should report -1 to indicate that the word is not found in
the list.
2) Binary search: Implement the block: find in sorted list. The block should accept a word
to search for and a list of words as input, and return the index of the word’s position in the
list. Searching through sorted data, while faster, also requires a more complicated block.
Below is the partially compete block to perform a binary search. It looks for word in list.
Notice that if the word is not found it will repeat until mix index &gt; max index.
See next page for an example…
Here's how things should generally work on a list of numbers:
1. The range of possibilities starts off including every number in the list (the minimum
is position #1 and the maximum is the final position). Let's say that we're searching
for the number 10 as shown in the diagram. Start your search around the middle of
the list. Check the value of the number there.
2. If the number at the spot you searched is greater than 10, set the maximum to the
slot one before the one you just searched. If the number is smaller than 10, set the
minimum to one after the one you searched.
3. Check the middle position of your new range.
4. Repeat step 2 for this new range and reduce the range further. Continue to repeat
steps 2 &amp; 3 until either (a) you find the number you're looking for or (b) you get in a
position where the minimum is greater than the maximum of the range.
min index
min index
index
max index
index
min index
max index
index
max index
max index
min index
Found at position 4!
index
When your code is complete run both searches 10 times
each. Keep track of the amount of time it took using each
strategy. Record the best time, worst time, and average
time for each.
```