5.4 - Searching Algorithms
Introduction to search algorithms
Search algorithms are sets of instructions that a computer follows to locate a specific item within a list or collection of data.
Linear search
A linear search is a straightforward algorithm that examines each item in a list sequentially until it finds the target or reaches the end.
The linear search algorithm
This checks each position starting from the beginning. It stops early if the target is found, or continues to the end if not.
Advantages and disadvantages of linear search
Advantages:
- Works on any list, whether ordered or unordered.
- Simple to program and understand.
Disadvantages:
- Inefficient for large lists, as it may need to check every item.
- Best used for small or unsorted datasets.
Representing linear search as a flow diagram
Flow diagrams use shapes to show the flow of an algorithm: ovals for start/stop, diamonds for decisions, rectangles for processes, and parallelograms for input/output.
For linear search:
- Start with an oval labelled "Start".
- A decision diamond asks: "Are there more items to search?".
- If "No", output "Item not found" in a parallelogram, then stop.
- If "Yes", a rectangle says "Check next item".
- Another decision diamond asks: "Is this the right item?".
- If "No", loop back to the first decision.
- If "Yes", output "Item found" in a parallelogram, then stop.
Worked example - Performing a linear search
Use a linear search to find the number 72 in this list: [15, 42, 72, 89, 27, 6].
Step 1: Start at the beginning
- Check position 0: 15 is not 72.
Step 2: Check next items
- Position 1: 42 is not 72.
- Position 2: 72 is 72.
Step 3: Conclude the search
- Item found at position 2.
- The search stops here without checking the remaining items.
Binary search
A binary search is a more advanced algorithm that efficiently finds items by repeatedly dividing an ordered list in half. It requires the list to be sorted beforehand, which allows it to eliminate large portions of the data quickly.
The binary search algorithm
Binary search uses a divide-and-conquer approach.
It calculates the middle position and compares. It adjusts the search range by half each time. With integer division, any decimal is rounded down (e.g., if low=0 and high=3, mid = (0+3)/2 = 1).
Requirements and limitations of binary search
- Must be ordered - The list needs to be sorted (e.g., ascending order) for the comparisons to work correctly.
- More complex - Harder to program than linear search due to the need to manage low/high pointers and midpoint calculations.
Binary search is ideal for large, sorted datasets where speed is crucial.
Worked example - Performing a binary search
Use a binary search to find the number 60 in this ordered list: [10, 20, 30, 40, 50, 60, 70].
Step 1: Find initial middle
- low = 0, high = 6
- mid = (0 + 6) / 2 = 3 (position 3, value 40).
- 40 < 60, so set low = 4, discarding 40 and before.
Step 2: Find new middle
- low = 4, high = 6
- mid = (4 + 6) / 2 = 5 (position 5, value 60).
- 60 matches the target.
Step 3: Final check
- Item found at position 5.
Comparing linear and binary search
Both linear and binary searches aim to find items in lists, but they differ in approach, efficiency, and requirements.
Binary search is generally more efficient, especially for bigger lists, as it avoids checking many items by discarding halves. However, if the list isn't sorted, linear search is the only option without first sorting (which adds extra steps).