You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers linear search and binary search — the two fundamental searching algorithms in the OCR A-Level Computer Science (H446) specification. Understanding when and why to use each is essential.
Searching is the process of finding a specific item (the target or key) within a collection of data. The two main approaches are:
| Algorithm | Description | Requirement |
|---|---|---|
| Linear search | Check each item one by one | Works on any data (sorted or unsorted) |
| Binary search | Repeatedly halve the search space | Requires sorted data |
Linear search (also called sequential search) checks each element in the collection one at a time, from the first to the last, until the target is found or the end is reached.
function linearSearch(data, target, size)
for i = 0 to size - 1
if data[i] == target then
return i
endif
next i
return -1
endfunction
def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i
return -1
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.