0% found this document useful (0 votes)
17 views

Notes - Linear and Binary Search

The document discusses two methods for searching arrays: linear search and binary search. It provides pseudocode algorithms and Java code examples for each method. Linear search sequentially checks each element until the target is found or all elements are checked. Binary search repeatedly halves the search space by checking the middle element and determining which half to search.

Uploaded by

Anupreet Kounsal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Notes - Linear and Binary Search

The document discusses two methods for searching arrays: linear search and binary search. It provides pseudocode algorithms and Java code examples for each method. Linear search sequentially checks each element until the target is found or all elements are checked. Binary search repeatedly halves the search space by checking the middle element and determining which half to search.

Uploaded by

Anupreet Kounsal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Notes - Searching

Two methods of searching an array are linear searching and binary searching.

Linear (Sequential) Search


Suppose we want to write an algorithm to get the computer to search through a list of numbers
(like a list of airline flight numbers). The list is ordered from the smallest to the biggest. The
easiest way to find our number is to start at the beginning and compare our number (which we
will call the target) to each number in the list. If we reach our target, then we are done. This
method of searching is called Linear Searching.

Here is the algorithm:


1. Start with the first item in the list.
2. Compare the current item to the target
3. If the current value matches the target then we declare victory and stop.
4. If the current value is less than the target then set the current item to be the next item and
repeat from 2.

Linear Search Java Code


1 public int linearSearch(int[] array, int key)
2 {
3 int size = array.length;
4 int index = 0;
5 while(index < size)
6 {
7 if(array[index] == key)
8 {
9 return index;
10 }
11 if(array[index] > key))
12 {
13 return -1;
14 }
15 index++;
16 }
17 return -1;
18 }

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

Is the index >= size of the array Æ no


Is the element at [0] equal to key Æ no
Is the element at [0] greater than key Æ no
Is the index >= size of the array Æ no
Is the element at [1] equal to key Æ no
Is the element at [1] greater than key Æ no
Is the index >= size of the array Æ no
Is the element at [2] equal to key Æ yes, so we return 2 because this is the
index were our key was found.

(Case: key not found before end) If we were to linear search the above array for the value 5:

Is the index >= size of the array Æ no


Is the element at [0] equal to key Æ no
Is the element at [0] greater than key Æ no

Is the index >= size of the array Æ no
Is the element at [2] equal to key Æ no
Is the element at [2] greater than key Æ yes, so we return -1 to indicate that the
value we are looking for cannot be in this
sorted list.

(Case: key not found after end) If we were to linear search the above array for the value 100:

Is the index >= size of the array Æ no


Is the element at [0] equal to our key Æ no

Is the element at [10] greater than our key Æ no
Is the index >= size of the array Æ yes, so we return -1 to indicate that the
value we are looking for was not found.

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.

Here is the algorithm:


1. Set the list to be the whole list
2. Find the middle value of the list
3. If the middle value is equal to the target then we declare victory and stop.
4. If the middle item is less than the target, then we set the new list to be the upper half of
the old list and we repeat from step 2 using the new list.
5. If the middle value is greater than the target, then we set the new list to be the bottom half
of the list, and we repeat from step 2 with the new list.

Binary Search Java Code


1 public int binarySearch(int[] array, int key)
2 {
3 int size = array.length;
4 int low = 0;
5 int high = size - 1;
6
7 while(high >= low) {
8 int middle = (low + high) / 2;
9 if(array[middle] == key) {
10 return middle;
11 }
12 if(array[middle] < key) {
13 low = middle + 1;
14 }
15 if(array[middle] > key) {
16 high = middle - 1;
17 }
18 }
19 return -1;
20 }
Explanation:
line 1: 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 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: low is the variable that tells us where the beginning of the remaining list is, and we give it
an initial value of 0.
line 5: high is the variable that tells us where the end of the remaining list is, and we give it an
initial value of the last thing in the list.
line 7: This line tells us to keep going until low is bigger than high.
line 8: middle we calculate so we can divide the list into two pieces.
lines 9 and 10: We found the key, so we return the index at which we found it.
lines 12 and 13: If the item in the middle of the list is less than our key, we should look for the
key in the top half of the list, so we calculate a new value for low.
lines 15 and 16: If the item in the middle of the list is greater than our key, we should look for
the key in the bottom half of the list, so we calculate a new value for high.
line 19: If we get to this line then we know that the key is not in the list so we return -1.

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

Set low = 0 and high = size -1 (in this case 10)


Set middle = (low + high) / 2 (in this case 5)
Is the element at [middle] equal to key Æ no // 12 != 6
Is the element at [middle] less than key Æ no // 12 > 6
Is the element at [middle] greater than key Æ yes, Set high = middle - 1 (in this case 4)
Is high greater than low Æ yes, continue… // 4 > 0

Set middle = (low + high) / 2 (in this case 2)


Is the element at [middle] equal to key Æ yes // 6 = 6
so, return 2 because that is the index at
which the value we are looking for is

(Case: key found) Ex 2: If we were to binary search the above array for the value 14:

Set low = 0 and high = size -1 (in this case 10)


Set middle = (low + high) / 2 (in this case 5)
Is the element at [middle] equal to key Æ no // 12 != 14
Is the element at [middle] less than key Æ yes // 12 < 14
so, Set low = middle + 1 (in this case 6)
Is the element at [middle] greater than key Æ no
Is high greater than low Æ yes, continue… // 10 > 6

Set middle = (low + high) / 2 (in this case 8)


Is the element at [middle] equal to key Æ no // 18 != 14
Is the element at [middle] less than key Æ no // 18 > 14
Is the element at [middle] greater than key Æ yes, Set high = middle - 1 (in this case 7)
Is high greater than low Æ yes, continue… // 7 > 6

Set middle = (low + high) / 2 (in this case 6)


Is the element at [middle] equal to key Æ yes // 14 = 14
so, return 6 because that is the index at
which the value we are looking for is

(Case: key not found in data) If we were to binary search the above array for the value 5:

Set low = 0 and high = size -1 (in this case 10)


Set middle = (low + high) / 2 (in this case 5)
Is the element at [middle] equal to key Æ no // 12 != 5
Is the element at [middle] less than key Æ no // 12 > 5
Is the element at [middle] greater than key Æ yes, Set high = middle - 1 (in this case 4)
Is high greater than low Æ yes, continue… // 4 > 0

Set middle = (low + high) / 2 (in this case 2)


Is the element at [middle] equal to key Æ no // 6 != 5
Is the element at [middle] less than key Æ no // 6 > 5
Is the element at [middle] greater than key Æ yes, Set high = middle - 1 (in this case 1)
Is high greater than low Æ yes, continue… // 1 > 0

Set middle = (low + high) / 2 (in this case 0)


Is the element at [middle] equal to key Æ no // 2 != 5
Is the element at [middle] less than key Æ yes // 2 < 5
so, Set low = middle + 1 (in this case 1)
Is the element at [middle] greater than key Æ no
Is high greater than low Æ no (1 is not greater than 1), so return -1
to indicate that the value we are looking
for was not found

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:

1. How does linear search work?


2. What 3 cases can happen?
3. Given a set of data [1, 2, 3, 4, 5], what checks are made when linear searching for the
value 4? How many elements are checked?
4. Given [1, 2, 3, 4, 5], what checks are made when linear searching for the value 8? How
many elements are checked?
5. What is the complexity of linear search?
6. How does binary search work?
7. What 2 cases can happen?
8. Give a set of data [11, 12, 13, 14, 15], what checks are made when binary searching for
the value 14? How many elements are checked?
9. Given [11, 12, 13, 14, 15], what checks are made when binary searching for the value
18? How many elements are checked?
10. What is the complexity of binary search?
11. What is something that must be true of a data set to be searched with binary search, but
does not need to be true for linear search? Why?

You might also like