The document discusses linear and binary search techniques. Linear search involves sequentially checking each element of an unsorted array to find a match, while binary search can only be used on sorted arrays and eliminates half of the remaining elements after each comparison. Pseudocode and examples are provided to illustrate the binary search algorithm and how it narrows down the search space at each step. The efficiency of binary search for large arrays is noted, taking at most 30 comparisons to search a billion element array. Exercises are provided to write function prototypes for bubble sort and binary search functions.
3. Linear Search Algorithm
Start at first element of array.
Compare value to value (key) for which you are searching
Continue with next element of the array until you find a
match or reach the last element in the array.
Note: On the average you will have to compare the search
key with half the elements in the array.
4. Linear Search Code
const int arraySize = 100;
int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87};
int key = 88;
bool found = false;
for (int i = 0; i < arraySize; i++)
{
if (a[i] = = key)
{
cout << “Found it at array subscript “ << i << endl;
found = true;
break;
}
}
if (! found)
cout << “Could not find element “ << key << “ in array a” << endl;
5. Binary Search Algorithm
1. May only be used on a sorted array.
2. Eliminates one half of the elements after each comparison.
3. Locate the middle of the array
4. Compare the value at that location with the search key.
5. If they are equal - done!
6. Otherwise, decide which half of the array contains the search
key.
7. Repeat the search on that half of the array and ignore the other
half.
8. The search continues until the key is matched or no elements
remain to be searched.
7. Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 19
use this as the middle of the array
Compare a[2] with 19
15 is smaller than 19 so use the top
half for the next pass
a
8. Binary Search Pass 3
3
4
5
25
27
search key = 19
use this as the middle of the array
Compare a[4] with 19
25 is bigger than 19 so use the bottom
half
a
19
9. Binary Search Pass 4
3
search key = 19
use this as the middle of the array
Compare a[3] with 19
Found!!
a
19
11. Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 18
use this as the middle of the array
Compare a[2] with 18
15 is smaller than 18 so use the top
half for the next pass
a
12. Binary Search Pass 3
3
4
5
25
27
search key = 18
use this as the middle of the array
Compare a[4] with 18
25 is bigger than 18 so use the bottom
half
a
19
13. Binary Search Pass 4
3
search key = 18
use this as the middle of the array
Compare a[3] with 18
Does not match and no more elements
to compare.
Not Found!!
a
19
14. const int arraySize = 13;
int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 };
int key = 23 , low = 0, middle, high = (arraySize - 1);
while (low <= high)
{
middle = (low + high) / 2;
if (key = = a[middle])
{
cout << “Found element “ << key << “ at index “ << middle << endl;
break;
}
else if (key < a [middle])
high = middle -1; // search low end of array
else
low = middle + 1; // search high end of array
}
15. Efficiency
Searching an array of 1024 elements will take at most
10 passes to find a match or determine that the
element does not exist in the array.
512, 256, 128, 64, 32, 16, 8, 4, 2, 1
An array of one billion elements takes a maximum of
30 comparisons.
The bigger the array the better a binary search is as
compared to a linear search
16. Exercises
1) Write a function prototype for a function that does a bubble
sort named BubbleSort.
2) Write a function prototype for a function that does a binary
search named BinarySearch. The return value for BinarySearch
is of type int. If the function successfully finds the key for which
it is searching the return value contains the index in the array of
the element that matches the key.
If the function does not find the key for which it is searching the
return value contains a -1
3) Write a main program that initializes an array with data that is
out of order, calls the BubbleSort function to sort it and then
calls the BinarySearch function to find an element in the array.