Standard Searching Algorithms (OCR GCSE Computer Science): Revision Notes
📚 Revision Notes
Standard Searching Algorithms
Searching algorithms are used to find a specific item in a list. The two main types are Linear Search and Binary Search. Each has different steps, uses, and efficiency depending on the dataset.
Linear Search
- A linear search examines each item in the list one by one.
- You start at the first item and check if it's the one you're looking for.
- If it's not, move to the next item and repeat until you find the item or reach the end of the list.
- This method works on unsorted lists.
Method
- Look at the first item in the list.
- If it matches the item you're searching for, stop.
- If not, move to the next item.
- Repeat this process until the item is found or the end of the list is reached.
Worked Example
infoNote
Given the list: 2, 0, 1, 7, 4, 3, 5, If you're searching for the number 7, you'll check each number (2, 0, 1) until you find it at the fourth position.
Binary Search
- A binary search is a divide-and-conquer algorithm.
- It requires the list to be sorted before searching.
- The list is repeatedly divided in half, which reduces the number of items to search through.
Method
- Ensure the list is sorted.
- Find the middle item in the list.
- If the middle item is what you're looking for, stop.
- If the item you're searching for is less than the middle item, search the left half of the list.
- If the item you're searching for is greater than the middle item, search the right half of the list.
- Repeat the process until the item is found or there are no items left to search.
Worked Example
infoNote
Given the sorted list: 6, 23, 45, 55, 67, 90, 92, 96, 99 If you're searching for 96, start with the middle item (67). Since 96 is greater, you search the right half: 90, 92, 96, 99.
The middle of this half is 92. Since 96 is greater, continue to the right.
The next middle item is 96, which matches your search.
Comparing Linear Search and Binary Search
| Linear Search | Binary Search |
|---|---|
| Can be used on unsorted lists. | Requires the list to be sorted first. |
| Slower for large lists as it checks every item one by one. | Faster for large lists, cutting the search area in half each time. |
| Simple to implement. | More efficient, but requires extra steps to sort the list first. |
infoNote
Key Points to Remember
- Linear Search works on both sorted and unsorted lists, but it is less efficient for larger lists.
- Binary Search requires the list to be sorted, but it is faster for large datasets because it halves the search space with each step.
- Binary Search is a better option when dealing with large, sorted lists. Linear search is simpler for small or unsorted lists.