Searching
Searching
s
Searching Algorithms
Success Criteria:
1. I can list the steps in a binary search algorithm.
2. I can use the binary search algorithm to search for
items in an ordered list.
3. I can list the steps in a linear search algorithm.
4. I can use the linear search algorithm to search for items
in an unordered list.
Search Algorithms
When searching for information within large amounts of data, a
computer uses algorithms in order to do this. There are 2 simple
methods that you need to be aware of:
7 21 52 59 68 92 94 99 133
There are 9 items in the list so the middle item is the (9+1) ÷ 2 = 5th item.
The 5th item is 68 and 68 <99 so get rid of the first half of the list to leave:
92 94 99 133
There are 4 items left so the middle item is the (4+1) ÷ 2 = 2.5 = 3rd item.
The 3rd item is 99. You’ve found the item you’re looking for so the search is
complete.
Task: Binary Search
Use the binary search algorithm to:
2 15 20 23 30 34 45 50 51
2. Find the number 145 from the list.
A linear search is much simpler than a binary search but less efficient.
A linear search can be used on any type of list, it doesn’t have to be
ordered. Due to it being inefficient, it is often only used on small lists.
Linear Search: The Steps
1. Look at the first item in the unordered list.
2. If this is the item you’re looking for, then stop the search – you’ve
found it!
3. If not, then look at the next item in the list.
4. Repeat steps 2 and 3 until you find the item that you’re looking
for or you’ve checked every item.
7 21 52 59 68 92 94 99 133
You’ve found the item you’re looking for so the search is complete.
Task: Linear Search
Use the linear search algorithm to:
2 15 20 23 30 34 45 50 51
2. Find the number 145 from the list.
3, 5, 9, 14, 17, 21, 27, 31, 35, 37, 39, 40, 42.