0% found this document useful (0 votes)
138 views4 pages

Linear Search Algorithm Explained

The document discusses a linear search algorithm, highlighting issues such as infinite loops due to improper variable management and the importance of initializing variables. It emphasizes the efficiency gained by using a flag variable to stop the loop when the search item is found. Additionally, it notes the benefits of using subroutines for code reusability and maintainability.

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)
138 views4 pages

Linear Search Algorithm Explained

The document discusses a linear search algorithm, highlighting issues such as infinite loops due to improper variable management and the importance of initializing variables. It emphasizes the efficiency gained by using a flag variable to stop the loop when the search item is found. Additionally, it notes the benefits of using subroutines for code reusability and maintainability.

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

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

The value of current will stay at 0. This means that the condition of the while loop will always be true
when the items list contains more than 0 elements, which will result in the loop running infinitely.
The variable index needs to be initialised otherwise there would be an error on line 8 if the search
item was not found as index would not have been defined.

A function returns a value whilst a procedure does not. This algorithm returns a value on line 13.

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

4 True

5 Steph False

7 3

4 True

5 Ahmed False

7 4

4 True
5 Keira True

6 4

7 5

4 True

5 Neelu False

7 6

4 False

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
Boolean

found f

The identifier found is much more meaningful than the identifier f as it signifies the purpose of the
variable, which is a flag to check if the search item has been found.

The while loop in Figure 2 stops repeating if the search item has been found. This can reduce the
number of comparisons that the algorithm needs to make and increases the efficiency.

The initial value -1 signifies the item has not been found.

● The subroutine can be used over and over again without repeating code.
● The subroutine can be used in different programs that require searching.
● The code is easier to read, maintain and debug.

You might also like