Linear and Binary Search Algorithms Homework
Linear and Binary Search Algorithms Homework
1. A binary search starts by finding the midpoint of the index of the ordered data and compares the value of
that index with the value you want to find. If the value if less than the value you are currently at, then you
eliminate the other half of the index, and vice versa. You then repeat the steps until you find the value
you are searching for, at which the search stops.
4. Linear search:
[2, 4, 7, 12, 15, 18, 20]
Compare each value with the desired value:
2 = 20? No
4 = 20? No
7 = 20? No
12 = 20? No
15 = 20? No
18 = 20? No
20 = 20? Yes
Value has been found
Binary search:
[2, 4, 7, 12, 15, 18, 20]
Midpoint = 12, so look at the upper half of list
[15, 18, 20]
Midpoint = 18, so look at the upper half of list
[20]
Value has been found
5. A binary search is generally more efficient because it is quicker for searching larger sets of data, as it
removes half of the set of data with each comparison. A linear search would have to compare each value
from beginning to end, which means it is either very efficient or very inefficient, making its efficiency
much more unreliable than a binary search.