Title: Searching Arrays
1Searching Arrays
- Linear search
- small arrays
- unsorted arrays
- Binary search
- large arrays
- sorted arrays
2Linear 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.
3Linear Search Code
const int arraySize 100 int aarraySize 1,
100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9,
87 int key 88 bool found false for
(int i 0 i lt arraySize i) if (ai
key) cout ltlt Found it at array
subscript ltlt i ltlt endl found true
break if (! found) cout
ltlt Could not find element ltlt key ltlt in array
a ltlt endl
4Binary Search Algorithm
May only be used on a sorted array. Eliminates
one half of the elements after each comparison.
Locate the middle of the array Compare the
value at that location with the search key. If
they are equal - done! Otherwise, decide which
half of the array contains the search key. Repeat
the search on that half of the array and ignore
the other half. The search continues until the
key is matched or no elements remain to be
searched.
5Binary Search Example
a
search key 19
1
0 1 2 3 4 5 6 7 8 9 10 11 12
5
15
19
25
27
29
middle of the array compare a6 and 19 19 is
smaller than 29 so the next search will use the
lower half of the array
31
33
45
55
88
100
6Binary Search Pass 2
a
search key 19
1
0 1 2 3 4 5
5
15
use this as the middle of the array Compare a2
with 19 15 is smaller than 19 so use the top
half for the next pass
19
25
27
7Binary Search Pass 3
search key 19
3 4 5
a
19
use this as the middle of the array Compare a4
with 19 25 is bigger than 19 so use the bottom
half
25
27
8Binary Search Pass 4
search key 19
3
a
use this as the middle of the array Compare a3
with 19 Found!!
19
9Binary Search Example
a
search key 18
1
0 1 2 3 4 5 6 7 8 9 10 11 12
5
15
19
25
27
29
middle of the array compare a6 and 18 18 is
smaller than 29 so the next search will use the
lower half of the array
31
33
45
55
88
100
10Binary Search Pass 2
a
search key 18
1
0 1 2 3 4 5
5
15
use this as the middle of the array Compare a2
with 18 15 is smaller than 18 so use the top
half for the next pass
19
25
27
11Binary Search Pass 3
search key 18
3 4 5
a
19
use this as the middle of the array Compare a4
with 18 25 is bigger than 18 so use the bottom
half
25
27
12Binary Search Pass 4
search key 18
3
a
use this as the middle of the array Compare a3
with 18 Does not match and no more elements to
compare. Not Found!!
19
13const int arraySize 13 int aarraySize 1,
2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100
int key 23 , low 0, middle, high
(arraySize - 1) while (low lt high)
middle (low high) / 2 if (key
amiddle) cout ltlt Found element ltlt
key ltlt at index ltlt middle ltlt endl
break else if (key lt a middle)
high middle -1 // search low
end of array else low
middle 1 // search high end of array
14Efficiency
Searching and 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
15Exercises
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.