0% found this document useful (0 votes)
4 views

A3 Worksheet – Linear Search Algorithm

The document presents two versions of a linear search algorithm in Python. The first version searches through a list of items to find a specified search item, returning the index if found, while the second version introduces a 'found' flag to terminate the search early once the item is located. Both implementations iterate through the list until the search item is found or the end of the list is reached.

Uploaded by

wba59179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

A3 Worksheet – Linear Search Algorithm

The document presents two versions of a linear search algorithm in Python. The first version searches through a list of items to find a specified search item, returning the index if found, while the second version introduces a 'found' flag to terminate the search early once the item is located. Both implementations iterate through the list until the search item is found or the end of the list is reached.

Uploaded by

wba59179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 def linear_search(items, search_item):

#
2 index = -1
3 current = 0

#
4 while current < len(items):

#
5 if items[current] == search_item:
6 index = current

#
7 current = current + 1

8 return index
items
search_item

index current items[current]

2 -1

3 0

4 True

5 Reg False

7 1

4 True

5 Chloe False

7 2
1 def linear_search(items, search_item):

#
2 index = -1
3 current = 0
4 found = False

#
#
5 while current < len(items) and found == False:

#
6 if items[current] == search_item:
7 index = current
8 found = True
#
9 current = current + 1

10 return index

found

found f

You might also like