Notes - Linear and Binary Search
Notes - Linear and Binary Search
Two methods of searching an array are linear searching and binary searching.
Explanation:
line 1: key is the target item that we will search for in data. The word int tells us that
linearSearch will return the index of the key if it finds the key in the list, and it will return
-1 (because this is not a valid index) if the key is not in the list.
line 3: size tells us the number of items that we have in the list.
line 4: index is the variable we will use to get the next item in the list, so we will give it an initial
value of 0 to start at the first element in the array.
line 5: This line says that we will keep repeating the lines 6 - 16 as long as index < size
line 7, 9: if we have found our key, then we return the index at which it was found
line 11, 13: if the item in the list has a greater value than our key (because our values are sorted
smallest to largest), we know the key cannot be in the list so we return -1.
line 15: We add one to index which lets us look at the next item in the list.
There are three cases that will occur when linear searching for an item in a sorted list:
1. The key will be found
2. The key will not be found before the end of the list.
3. The key will not be found until the end of the list. (This is the worst case because we
have to look at every single item in the list to discover that what we are looking for is
not there.)
(Case: key found) If we were to linear search the following array for the value 6:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
(Case: key not found before end) If we were to linear search the above array for the value 5:
(Case: key not found after end) If we were to linear search the above array for the value 100:
The previous code given for linear search is actually a modified version of linear search which
works more efficiently because the data is guaranteed to already be sorted. Linear search will
work with unsorted data, but a change must be made to the code provided.
We must remove:
11 if(array[index] > key))
12 {
13 return -1;
14 }
which will cause the linear search to be less efficient, but still work. The only thing that has
changed is that we are no longer depending on the data to be in order to search it, because we no
longer stop looking when we find an element greater than the value we are looking for. Instead
we will only stop looking if the value is found or we reach the end of the list.
Binary Search
How do you look for a name in the phone book? You open it up somewhere in the middle and
then see if you need to go forward or backward. The computer needs more precise instructions
than this so we will tell it to divide the list exactly in half and compare the middle item to our
target. If the middle item is smaller than our target, then we can look at the top half of the list. If
it is bigger than our target, we will tell the computer to look in the bottom half of the list. We call
this kind of search Binary Searching because we always divide the number of items we will
search in half.
There are two cases that will occur when binary searching for an item in a sorted list:
1. The key will be found
2. The key will not be found before low > high.
(Case: key found) Ex 1: If we were to binary search the following array for the value 6:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
(Case: key found) Ex 2: If we were to binary search the above array for the value 14:
(Case: key not found in data) If we were to binary search the above array for the value 5:
Binary search requires that our data be sorted, and unlike linear search, there is no way to
modify the code to accommodate unsorted data.
Binary search works with a complexity of O(log n), which means that in its worst case, we
will search log n (where n is the size of the data set) elements in order to find out if what we are
searching for is or is not in the data set.
Linear search works with a complexity of O(n), which means that in its worst case, we will
search n elements in order to find out if what we are searching for is or is not in the data set.
In the worst case, binary search is always more efficient, though there are certain cases where
linear search would be more efficient.
Linear Search:
Processing Time
Processing Time
Data Size 1 Data Size
Binary Search:
Notice that the larger the data set grows the less efficient linear search becomes in comparison to
binary search.
Comprehension Questions: