Knowledge organisersSearching and sorting algorithms
Understand the main steps of each algorithm, Understand any pre-requisites of an algorithm, Apply the algorithm to a data set, Identify an algorithm if given the code or pseudocode for it
Linear search is a simple searching algorithm that checks each item in a list one by one, starting from the first item, until the target is found or the end of the list is reached. It works on both sorted and unsorted lists, making it versatile but slower than binary search for large datasets.
False / -1 or 'not found'.theTeam[count].def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i # Found at index i
return -1 # Not found
numbers = [15, 7, 3, 5, 13, 8]
print(linear_search(numbers, 13)) # Output: 4
print(linear_search(numbers, 6)) # Output: -1