Searching Algorithms
Algorithms
Consider these three searching algorithms.
- Binary Search
- A sorted (ordered) list is required for a binary search to work.
The general algorithm is the same for ascending or descending order,
however you will need to know which sorted order is being used.
Our discussion here will focus on ascending order.
- Jump to the middle of list and examine the element at this position.
If you have found the element desired, return its index location
Otherwise, test to see if the location is less than the desired value
and if so search to the right of this midpoint. Note, the elements
stored in the index positions greater than the midpoint will all be
greater than the element value of the midpoint since the list is
ascending.
- If the midpoint location is greater than the element desired, then
search to the left of the midpoint. The lesser values are all in
index positions less than the midpoint. When searching to the left
or the right, examine the midpoint to the left or the right (note,
this will be the quarter point of the entire list). Repeat the
process of determining to move left or right and jumping to a
sub-midpoint until there are no more sub-midpoints to examine.
- The search will end when the index position of the desired element
is found or when all of the sub-midpoints have been searched.
- Consider this sequence of numbers:
Step #
| Index
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
|
Value
| 10
| 13
| 23
| 25
| 26
| 31
| 45
| 47
| 54
| 55
| 59
| 63
| 87
| 90
| 95
|
- Search for the value 26 within the example list. Note, index
7 is the midpoint and our two end points are index
0 and 14. Since 26 is
less than the midpoint value (47), we move the left. Our new sub-midpoint
and sub end points are shown in step two.
Step #1
| Index
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
|
Value
| 10
| 13
| 23
| 25
| 26
| 31
| 45
| 47
| 54
| 55
| 59
| 63
| 87
| 90
| 95
|
Step #2
| Index
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
|
Value
| 10
| 13
| 23
| 25
| 26
| 31
| 45
| 47
| 54
| 55
| 59
| 63
| 87
| 90
| 95
|
- Now practice searching for the value 89.
- Observe in the best case we will find the desired value in the first index
position we examine, giving us Θ(1) time complexity.
In the average and worst case it will take us Θ(log2 n)
time complexity to complete the search. Where n is the size of our list.
- Linear Search
- Start at the beginning of a list, examine the value at each index position
one at a time until reaching the other end of the list. If along the
way we find the desired element, return its index position. Otherwise,
if we reach the end of the list, the element is not inside our list.
- Observe in the best case we will find the desired value in the first index
position we examine, giving us Θ(1) time complexity.
In the average case we will search half of the list Θ(n / 2) time complexity.
Finally, in the worst case we will search the entire list for Θ(n) time
complexity. Where n is the size of our list.
- Random Search
- Use a random number generator to create index locations to examine.
If the desired element is found, return the current index location.
Otherwise, generate a new random index and repeat the process.
- When will this algorithm stop? How do you determine when it might stop?
- What are the best, average, and worst case time complexities of this algorithm?