Topic 7.4 Linear Search & Bubble Sort
Topic 7.4 Linear Search & Bubble Sort
4 REPEAT UNTIL you have checked all values and not found the value you are looking for
Page 3
A linear search in Pseudocode
# Identify the dataset, target value, and set the initial flag
DECLARE data : ARRAY[1:5] OF INTEGER
DECLARE target : INTEGER Your notes
DECLARE found : BOOLEAN
data ← [5, 2, 8, 1, 9]
target ← 11
found ← FALSE
# Identify the dataset to search, the target value and set the initial flag
data = [5, 2, 8, 1, 9]
target = 11
found = False
4 REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)
Your notes
Example
Perform a bubble sort on the following dataset
5 2 4 1 6 3
Step Instruction
5 2 4 1 6 3
2 5 4 1 6 3 Your notes
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
2 1 4 3 5 6
1 2 3 4 5 6
Your notes
End of pass 4 (no swaps)
1 2 3 4 5 6
# Unsorted dataset
DECLARE nums : ARRAY[1:11] OF INTEGER
nums ← [66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]
# Unsorted dataset
nums = [66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]
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
Page 10
Pseudocode
Your notes
Count ← 50
DO
OUTPUT “Pass number”, Count
Count ← Count - 1
UNTIL Count <= 0
Pseudocode
Total ← 0
Scores ← [25, 11, 84, 91, 27]
Highest ← max(Scores)
Lowest ← min(Scores)
# Calculate average
Average ← Total / LENGTH(Scores)