Book 8 · Chapter 9
Algorithm design
B8·9This chapter is being written.
Everything the site already has for it is below — each with the short code the book prints beside it.
For the whole chapter
- Topic pageAlgorithm design (2029–2031)
B8·9·T - QuizAlgorithm design
B8·9·Q1 - LabFlowchart Studio
B8·9·X1 - LabAlgorithm Lab — sorting
B8·9·X2 - LabAlgorithm Lab — searching
B8·9·X3 - LabPython example py-linear-search
B8·9·X4 - LabPython example py-binary-search
B8·9·X5 - LabPython example py-bubble-sort
B8·9·X6 - LabPython example py-insertion-sort
B8·9·X7 - LabPython example py29-merge-sort
B8·9·X8 - LabPython example py29-compare-searches
B8·9·X9 - LabPython example py29-logic-error
B8·9·X10
§9.3 Algorithm design and evaluation
Short code B8·9·3
- FlashcardsFlashcards §9.3: Flowcharts, trace tables & fixing algorithms
B8·9·F3 - FlashcardsFlashcards §9.3: Linear and binary search
B8·9·F4 - FlashcardsFlashcards §9.3: Bubble, insertion and merge sort
B8·9·F5
Abstraction: keep only what matters
Short code B8·9·10
# The model: only what the journey-time question needs
stops = ["Gate", "Library", "Canteen", "Sports hall"]
# minutes from each stop to the next one
minutes = [3, 4, 6]
start = int(input())
finish = int(input())
total = 0
for i in range(start, finish):
total = total + minutes[i]
print("From", stops[start], "to", stops[finish])
print("Journey time:", total, "minutes")Output
From Gate to Sports hall Journey time: 13 minutes
Decomposition: inputs, processes, outputs, storage
Short code B8·9·11
# Storage
houses = ["Red", "Blue", "Green"]
totals = [0, 0, 0]
for event in range(2):
for house in range(3):
# Input, checked: 0 to 10 only
points = int(input())
while points < 0 or points > 10:
points = int(input())
# Process: add to the house total
totals[house] = totals[house] + points
# Process: find the winner
best = 0
for house in range(1, 3):
if totals[house] > totals[best]:
best = house
# Output
for house in range(3):
print(houses[house], totals[house])
print("Winner:", houses[best])Output
Red 15 Blue 11 Green 7 Winner: Red
Flowcharts: the five symbols
Short code B8·9·12
passes = 0
for i in range(5):
mark = int(input())
if mark >= 50:
passes = passes + 1
print(passes)Output
3
Designing and amending an algorithm
Short code B8·9·13
how_many = int(input()) # change 1
passes = 0
fails = 0 # change 2
for i in range(how_many): # change 1
mark = int(input())
if mark >= 50:
passes = passes + 1
else: # change 2
fails = fails + 1
print(passes)
print(fails) # change 3Output
2 1
Explaining what an algorithm does
Short code B8·9·14
count = 0
total = 0
while True:
number = int(input())
if number == -1:
break
if number % 2 == 0:
total = total + number
count = count + 1
print(count, total)Output
3 20
Trace tables
Short code B8·9·15
Output
3 20
total = 0
for day in range(1, 5):
steps = int(input())
if steps >= 8000:
total = total + 1
print("Day", day, "target met")
print(total, "days")Output
Day 1 target met Day 3 target met Day 4 target met 3 days
Output
1 9200 1 | Day 1 target met 2 6500 - | 3 8000 2 | Day 3 target met 4 12000 3 | Day 4 target met output: 3 days
Output
start 4073 0 407 1 40 2 4 3 0 4 output: 4
Output
start 5 0 3 5 1 8 output: 8
Finding and fixing errors
Short code B8·9·16
Output
Average: 1.5 Largest: 4
Output
Average: 7.5 Largest: 10
total = 0
for n in range(5):
number = int(input())
total = total + number
print("Total:", total)Output
Total: 20
Linear search
Short code B8·9·17
names = ["Hina", "Ali", "Sara", "Omar", "Zoya"]
target = input()
found = False
index = 0
while index < len(names) and not found:
if names[index] == target:
found = True
else:
index = index + 1
if found:
print(target, "found at index", index)
else:
print(target, "not found")index is only moved on while nothing has been found. Input: Omar.Output
Omar found at index 3
names = ["Hina", "Ali", "Sara", "Omar", "Zoya"]
target = input()
found = False
index = 0
while index < len(names) and not found:
if names[index] == target:
found = True
else:
index = index + 1
if found:
print(target, "found at index", index)
else:
print(target, "not found")index reaches 5.Output
Zara not found
codes = [17, 44, 31, 8, 52, 26]
target = int(input())
index = 0
found = False
while True:
if codes[index] == target:
found = True
else:
index = index + 1
if found or index == 6:
break
if found:
print("Found at", index)
else:
print("Not found")Output
Found at 2
Binary search
Short code B8·9·18
nums = [4, 9, 15, 22, 31, 38,
46, 53, 60, 71, 85]
target = int(input())
low = 0
high = len(nums) - 1
found = False
while low <= high and not found:
mid = (low + high) // 2
print(low, high, mid, nums[mid])
if nums[mid] == target:
found = True
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
if found:
print("Found at index", mid)
else:
print("Not found")Output
0 10 5 38 6 10 8 60 Found at index 8
Output
0 10 5 38 0 4 2 15 3 4 3 22 Not found
Bubble sort
Short code B8·9·19
Output
1 [147, 152, 139, 155, 160] True 2 [147, 139, 152, 155, 160] True 3 [139, 147, 152, 155, 160] True 4 [139, 147, 152, 155, 160] False
Output
Basic: 16 comparisons Improved: 10 comparisons
scores = [23, 8, 15, 4]
swapped = True
while swapped:
swapped = False
for i in range(len(scores) - 1):
if scores[i] > scores[i + 1]:
temp = scores[i]
scores[i] = scores[i + 1]
scores[i + 1] = temp
swapped = True
print(scores)Output
[4, 8, 15, 23]
names = ["Musa", "Alia", "Zain", "Dua"]
swapped = True
while swapped:
swapped = False
for i in range(len(names) - 1):
if names[i] < names[i + 1]:
temp = names[i]
names[i] = names[i + 1]
names[i + 1] = temp
swapped = True
print(names)Output
['Zain', 'Musa', 'Dua', 'Alia']
Insertion sort
Short code B8·9·20
Output
insert 12 [12, 31, 47, 5, 26, 19] insert 47 [12, 31, 47, 5, 26, 19] insert 5 [5, 12, 31, 47, 26, 19] insert 26 [5, 12, 26, 31, 47, 19] insert 19 [5, 12, 19, 26, 31, 47]
Merge sort
Short code B8·9·21
Output
[41, 18, 66, 7, 53, 29, 12, 90] [41, 18, 66, 7] [53, 29, 12, 90] [41, 18] [66, 7] [53, 29] [12, 90] [41] [18] [66] [7] [53] [29] [12] [90] [18, 41] [7, 66] [29, 53] [12, 90] [7, 18, 41, 66] [12, 29, 53, 90] [7, 12, 18, 29, 41, 53, 66, 90]
Comparing algorithms
Short code B8·9·22
Output
3 3 8 500 500 1 1000 1000 10
Command-word coach
Short code B8·9·23
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Workbook: Algorithm design
Short code W8·9 · Algorithms, Programming and Databases Workbook
The write-in workbook for this chapter — drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.
Starter files
Short code W8·9·K
Data files and skeleton code in Python for this chapter's tasks arrive here with the chapter.


