Standard Algorithms: Linear Search
| Created | |
|---|---|
| Tags |
This searches an array for a particular value.
It will return:
- The position of the user’s input in the array, If successful.
- A message saying the item was never found, if unsuccessful.
graph TD
A[Start Search] --> B{Item Found?};
B -->|Yes| C[Return Success];
B -->|No| D[Return Not Found];
marks = [12,15,50,22,11,88,50,90,42,33] #sets up an array full of exam marks.
student_mark = int(input("What mark would you like to search for?")) #this is the input that is fed into the linear search algorithm.
for item in range(10): #repeat as there are 10 numbers
if marks[item] == student_mark: #if there is a match
print("Student ",item,"has achieved mark", student_mark)marks = [12,15,50,22,11,88,50,90,42,33]
found = False # A statement that is checked at the end to determine the message.
student_mark = int(input("What mark would you like to search for?"))
for item in range(10):
if marks[item] == student_mark:
print("Student ",item,"has achieved mark", student_mark)
found = True
if not found: # so if the if loop does not run, print the failure message.
print("Mark",student_mark,"Is not present in the list of marks\n")