Linear Search
Linear Search
Computer Science
Standard Methods of a Solution
Contents
Linear Search & Bubble Sort
Other Standard Methods of a Solution
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 1
Linear Search & Bubble Sort
Your notes
Linear Search
What is a searching algorithm?
Searching algorithms are precise step-by-step instructions that a computer can follow
to efficiently locate specific data in massive datasets
4 REPEAT UNTIL you have checked all values and not found the value you are looking
for
// Declare variables
DECLARE data : ARRAY[1:5] OF INTEGER
DECLARE target : INTEGER
DECLARE found : BOOLEAN
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 2
target ← 11
found ← FALSE // Start with the assumption that the target is not found
Your notes
// Loop through each element in the array
FOR index ← 1 TO 5
// Check if the current element matches the target
IF data[index] = target THEN
found ← TRUE
OUTPUT "Target found"
ENDIF
NEXT index
# Identify the dataset to search, the target value and set the initial flag
data = [5, 2, 8, 1, 9]
target = 11
found = False
Bubble Sort
What is a sorting algorithm?
Sorting algorithms are precise step-by-step instructions that a computer can follow
to efficiently sort data in massive datasets
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 3
How do you perform a bubble sort?
Your notes
Step Instruction
4 REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)
Example
Perform a bubble sort on the following dataset
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 4
5 2 4 1 6 3
Your notes
Step Instruction
5 2 4 1 6 3
2 5 4 1 6 3
2 5 4 1 6 3
4 REPEAT step 2 & 3 until you reach the end of the dataset
5 & 4 SWAP!
2 4 5 1 6 3
5 & 1 SWAP!
2 4 1 5 6 3
5 & 6 NO SWAP!
2 4 1 5 6 3
6 & 3 SWAP!
2 4 1 5 3 6
End of pass 1
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 5
2 1 4 3 5 6
Your notes
End of pass 3 (swaps made)
1 2 3 4 5 6
1 2 3 4 5 6
// Loop through the array from the start to the second-last unsorted element
FOR y ← 1 TO numlength - 1
// If the current number is greater than the next number, swap them
IF nums[y] > nums[y + 1] THEN
DECLARE temp : INTEGER
temp ← nums[y]
nums[y] ← nums[y + 1]
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 6
nums[y + 1] ← temp
# Unsorted dataset
nums = [66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]
Worked Example
A program uses a file to store a list of words.
Show the stages of a bubble sort when applied to data shown [2]
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 7
How to answer this question
We need to sort the values in to alphabetical order from A-Z Your notes
You CAN use the first letter of each word to simplify the process
Answer
E, B, C, M, G, P (pass 1)
B, C, E, G, M, P (pass 2)
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 8
Other Standard Methods of a Solution
Your notes
Totalling & Counting
What is totalling?
Totalling is keeping a running total of values entered into the algorithm
An example may be totalling a receipt for purchases made at a shop
In the example below, the total starts at 0 and adds up the user inputted value for each
item in the list
Pseudocode
Total ← 0
FOR Count ← 1 TO ReceiptLength
INPUT ItemValue
Total ← Total + itemValue
NEXT Count
OUTPUT Total
What is counting?
Counting is when a count is incremented or decremented by a fixed value, usually 1,
each time it iterates
Counting keeps track of the number of times an action has been performed
Many algorithms use counting, including the linear search to track which element is
currently being considered
In the example below, the count is incremented and each pass number is output until
fifty outputs have been produced
Pseudocode
Count ← 0
DO
OUTPUT “Pass number”, Count
Count ← Count + 1
UNTIL Count >= 50
In the example below, the count is decremented from fifty until the count reaches zero.
An output is produced for each pass
Pseudocode
Count ← 50
DO
OUTPUT “Pass number”, Count
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 9
Count ← Count - 1
UNTIL Count <= 0
Your notes
Pseudocode
Total ← 0
Scores ← [25, 11, 84, 91, 27]
Highest ← max(Scores)
Lowest ← min(Scores)
# Calculate average
Average ← Total / LENGTH(Scores)
© 2025 Save My Exams, Ltd. Get more and ace your exams at savemyexams.com 10