7.4 - Search Algorithms
Introduction to search algorithms
Search algorithms are step-by-step processes that computers use to locate specific items within a list of data. The two main types are linear search and binary search.
An ordered list is one where items are arranged in a specific sequence, such as numerical or alphabetical order. An unordered list has no particular sequence.
How linear search works
Linear search is a straightforward algorithm that examines each item in a list sequentially until it finds the target or reaches the end.
Steps in a linear search algorithm
- Look at the first item in the list.
- If this item matches the target value, stop the search as the item has been found.
- If not, look at the next item in the list.
- Repeat steps 2 and 3 until the item is found or all items have been checked.
This method is simple to implement and does not require the list to be ordered.
Example of linear search
Use a linear search to find the number 99 from the list: [7, 21, 52, 59, 68, 92, 94, 99, 133].
Step-by-step process:
- Check first item: 7 ≠ 99
- Look at the next item: 21 ≠ 99
- Look at the next item: 52 ≠ 99
- Look at the next item: 59 ≠ 99
- Look at the next item: 68 ≠ 99
- Look at the next item: 92 ≠ 99
- Look at the next item: 94 ≠ 99
- Look at the next item: 99 = 99
The search stops here as the item is found.
Linear search is often only used on small lists.
How binary search works
Binary search is an efficient algorithm that works by repeatedly dividing an ordered list in half to narrow down the possible location of the target item. It requires the list to be sorted in advance.
Steps in a binary search algorithm
- Find the middle item in the current ordered list. To find the middle item in a list of n items, calculate the position as (n + 1) ÷ 2, rounding up if necessary.
- If this is the item you are looking for, stop the search as the item is found.
- If not, compare the middle item to the target value. If the target is less than the middle item, discard the second half of the list. If the target is greater than the middle item, discard the first half of the list.
- Repeat steps 1-3 on the remaining list until the item is found.
This process reduces the search space by half each time.
Example of binary search
Use binary search to find the number 99 in this ordered list: [7, 21, 52, 59, 68, 92, 94, 99, 133].
Step-by-step process:
- There are 9 items, so middle position = (9 + 1) ÷ 2 = 5th item (68).
- 68 < 99, so discard the first half: new list [92, 94, 99, 133].
- Now 4 items, middle position = (4 + 1) ÷ 2 = 2.5, rounded up to 3rd item (99).
- Middle item is 99, which matches the target.
The search completes after a few steps.
Comparing linear and binary search
Both algorithms aim to find items in lists, but they differ in approach, requirements, and performance.
Key differences between linear and binary search
- List requirements - Linear search works on any list (ordered or unordered), while binary search requires an ordered list.
- Efficiency - Linear search checks items one by one. Binary search halves the list each time, making it much more efficient for large lists.
- Simplicity - Linear search is much simpler than a binary search. Binary search is more complex.
- Use cases - A linear search is often only used on small lists. Once the list has been ordered, a binary search is much more efficient than a linear search and is more suitable for large lists of items.