GCSE J277 Unit 2.
1 | Algorithms Craig’n’Dave
Name:
Specification and learning objectives
By the end of this topic, you will have studied:
• Principles of computational thinking: Abstraction, decomposition, algorithmic thinking
• The inputs, processes and outputs of a problem
• Structure diagrams
• Creating, interpreting, correcting and refining algorithms using: Pseudocode, flowcharts, reference/high-level language
• Identifying common errors
• Trace tables
• Standard searching algorithms: Binary search, linear search
• Standard sorting algorithms: Bubble sort, merge sort, insertion sort
Resources
We recommend the OCR-endorsed text book from PG Online for use during your GCSE studies.
Craig ‘n’ Dave videos for SLR 2.1
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Abstraction
Abstraction means:
Example of abstraction:
Real aeroplane: Paper aeroplane: Necessary features of a paper aeroplane:
Unnecessary features of a paper aeroplane:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Abstraction
Cat: Cat icon: Necessary features of the icon: Unnecessary features of the icon:
Dog: Dog icon: Necessary features of the icon: Unnecessary features of the icon:
Rabbit: Rabbit icon: Necessary features of the icon: Unnecessary features of the icon:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Abstraction
A computer program outputs whether a capital city in Europe is north or south of another capital city in Europe. It only needs to know the latitude of the two
cities – the other detail is unnecessary. This is an example of abstraction; including the necessary detail and not including the unnecessary detail.
City Latitude (N)
Dublin 53.3498
London 51.5074
Oslo 59.9139
Paris 48.8566
Madrid 40.4168
Program:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Decomposition
Decomposition means:
Examples of problem decomposition in every-day life:
Making toast: Making a fairy cake:
[Picture of toast [Picture of fairy
here] cake here]
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Decomposition and structure diagrams
Advantages of decomposition include:
Example of problem decomposition in calculating the cost of
producing costume jewellery:
Red beads
Chain
Purple beads
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Decomposition and structure diagrams
The advantages of using structure diagrams when designing a solution are:
Wages program
Calculate
Calculate wage Print wage slip
deductions
Calculate basic
Calculate tax
wage
Calculate
Calculate
National
overtime
Insurance
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Decomposition and structure diagrams
Structure diagram for a pupil management program:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Identify the input, processes and outputs for a problem
An input is:
A process is:
An output is:
Title of program What does it do? Inputs Processes Outputs
width : real
Swimming pool volume Calculates the total volume length : real Volume = width * length * volume : real
of a swimming pool depth
depth : real
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Algorithmic thinking
Decomposition of pick-up sticks:
Program:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Algorithmic thinking
Decomposition of noughts and crosses:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Linear search
Explanation of a linear search:
Steps to find the Geography
book using a linear search:
Pseudocode of the linear book = ["Archaeology", "Art", "Biology", "Chemistry", "Computing", "English", "French",
search algorithm: "Geography", "History", "Maths", "Psychology"]
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Binary search
Explanation of a binary search:
Steps to find the Geography
book on the shelf using a
binary search:
Special condition for a binary search to work:
In most cases, the quicker search is the: algorithm. However, this is not true if the first item in the list is the one being searched for.
If the item you want to find is first in the list, the algorithm would be quicker.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Binary search
Pseudocode of the binary book = ["Archaeology", "Art", "Biology", "Chemistry", "Computing", "English", "French",
search algorithm: "Geography", "History", "Maths", "Psychology"]
found = False
left = 0
right = LEN(book)-1
find = "Geography"
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Bubble sort
How a bubble sort works: Note how 32 has “bubbled” to the top – The algorithm has been optimised, so it does not check the
this is how the bubble sort got its name. numbers already bubbled to the top. It can also stop if no
swaps are made after all the numbers are checked.
24 32
8 24
16 16
2 8
32 2
Check 2 Check 32 Check 32 Check 32 Check 2 Check 16 Check 16 Check 2 Check 8 Check 2
and 32 and 16 and 8 and 24 and 16 and 8 and 24 and 8 and 16 and 8
Swap Swap Swap Swap No swap Swap No swap No swap No swap No swap
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Merge sort
How a merge sort works:
82
43
27
38
Original list Merge adjacent Until all lists
lists together are merged
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Merge sort
How a merge sort works:
24
16
32
Original Split into Swap numbers Merge adjacent lists together Merge adjacent lists together
list adjacent sub- if necessary in by comparing the first number by comparing the first number
lists of up to two each sub-list in each list, moving the in each list and moving the
numbers 8 and 16 swap smaller number into a new smaller number into a new
list, one number at a time list, one number at a time
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Insertion sort
How an insertion sort works:
24
Yellow dotted box:
unsorted data
16
32
Green solid box:
sorted data
? inserted ? inserted ? inserted ? inserted ? inserted
in place in place in place in place in place
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Flowchart symbols
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
How to produce algorithms using flowcharts
An algorithm for a role-playing game
displays three choices from a menu
to the user:
1. Play game
2. Change character
3. Quit
The user input is validated so only
the numbers 1 – 3 can be entered.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Interpret, correct, refine or complete algorithms.
An algorithm for a role-playing game do
displays three choices from a menu print("1. Play game")
to the user: print("2. Change character")
1. Play game print("3. Quit")
2. Change character
3. Quit input(int(choice))
The user input is validated so only until choice<1 AND choice>4
the numbers 1 – 3 can be entered.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
How to produce algorithms using flowcharts
An algorithm for a role-playing game
handles a battle between two player
characters.
Each character has an attack and
defence attribute that must be input
by the user before an engagement.
When the two characters engage, a
random number between 1 and 12 is
generated for each player.
The attack attribute plus the defence
attribute is added to the player's roll.
If player 1’s total is greater than
player 2’s total, player 1 wins –
otherwise, player 2 wins.
The winner is output.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
How to produce algorithms using pseudocode (OCR reference language)
An algorithm for a role-playing game
handles a battle between two player
characters.
Each character has an attack and
defence attribute that must be input
by the user before an engagement.
When the two characters engage, a
random number between 1 and 12 is
generated for each player.
The attack attribute plus the defence
attribute is added to the player's roll.
If player 1’s total is greater than
player 2’s total, player 1 wins –
otherwise, player 2 wins.
The winner is output.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Interpret, correct, refine or complete algorithms
Modified algorithm to correct an
issue with player 2 winning more
battles than player 1:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Interpret, correct, refine or complete algorithms
An algorithm for a role-playing game function randomcaverns(n)
generates a list of random caverns caverns = []
into which objects will be placed. for c = 1 to n
valid = TRUE
Caverns are numbered from 1 – 50. while valid = FALSE
The number of caverns to return is n. r = random(1,50)
valid = FALSE
for i = 0 to caverns.LENGTH
if caverns[i] = r then valid = FALSE endif
next i
endwhile
caverns[c] = c
next c
return caverns
endfunction
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
How to produce algorithms using flowcharts
A role-playing game allows a player
to input their next move by entering
N, E, S or W. Valid moves are stored
in a list like this: move = [0,1,0,1]
Zero means the move is not possible.
One means it is possible. The
possibilities are stored in the list in
the order: N, E, S, W.
A function takes two parameters:
m is the move: N, E, S or W. vm is a
list of the valid moves.
Assume a zero-indexed list/array.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
How to produce algorithms using pseudocode (OCR reference language)
A role-playing game allows a player
to input their next move by entering
N, E, S or W. Valid moves are stored
in a list like this: move = [0,1,0,1]
Zero means the move is not possible.
One means it is possible. The
possibilities are stored in the list in
the order: N, E, S, W.
A function takes two parameters:
m is the move: N, E, S or W. vm is a
list of the valid moves.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Identifying common errors and suggesting fixes
01: input (int(p1attack))
02: input (int(p1defence))
03: input (int(p2attack))
04: input (int(p2defence))
05:
06: dice = random(1,12)
07: total1 = dice + p1attack + p1defence
08:
09: dice = random(1,12)
10: total2 = dice + p2attack + p2defence
11:
12: if total1 > total2 then
13: print(Player 1 wins)
14: else
15: print("Player 2 wins")
16: endif
The error is on line:
The type of error is:
To fix this error:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Identifying common errors and suggesting fixes
01: input (int(p1attack))
02: input (int(p1defence))
03: input (int(p2attack))
04: input (int(p2defence))
05:
06: dice = random(1,12)
07: total1 = dice + p1attack + p1defence
08:
09: dice = random(1,12)
10: total2 = dice + p2attack + p2defence
11:
12: if total1 < total2 then
13: print("Player 1 wins")
14: else
15: print("Player 2 wins")
16: endif
The error is on line:
The type of error is:
To fix this error:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Identifying common errors and suggesting fixes
01: input (int(p1attack))
02: input (int(p1defence))
03: input (int(p2attack))
04: input (int(p2defence))
05:
06: dice = random(1 12)
07: total1 = dice + p1attack + p1defence
08:
09: dice = random(1 12)
10: total2 = dice + p2attack + p2defence
11:
12: if total1 > total2 then
13: print("Player 1 wins")
14: else
15: print("Player 2 wins")
16: endif
The error is on line:
The type of error is:
To fix this error:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Trace tables
Assuming the user enters the values:
01: x = int(input("Enter a number: "))
• Line 01: x = 2
02: y = int(input("Enter another number: "))
• Line 02: y = 7
03:
complete the trace table for this program.
04: total = x
05:
06: for counter = 1 to y
X=2 Y=7 total counter 07: total = total * x
08: print(total)
09: next counter
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Assessment Target: Overall grade:
Minimum expectations by the end of this unit
Learn terms 134-151 from GCSE Level Key Terminology.
Complete all pages of the workbook.
Score 80% in the end-of-unit test.
Feedback
Breadth Depth Understanding
All aspects complete Excellent level of depth All work is accurate
Most aspects complete Good level of depth Most work is accurate
Some aspects complete Basic level of depth shown Some work is accurate
Little work complete Little depth and detail provided Little work is accurate
Comment and action Student response
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Reflection and Revision checklist
Confidence Clarification
I can explain what is meant by the term abstraction.
I can explain why abstraction is helpful when designing a solution to a problem.
I can explain what decomposition is and how it is useful.
I can explain what is meant by algorithmic thinking.
I can identify the inputs, processes and outputs of a problem.
I can use structure diagrams to help design a solution to a problem.
I can identify common errors.
I can explain what trace tables are and how to use them.
I can explain how a binary search works.
I can explain how a linear search works.
I can explain how a bubble sort works.
I can explain how a merge sort works.
I can explain how an insertion sort works.
I can explain how to produce pseudocode to describe an algorithm and why it is needed.
I can explain how to produce a flowchart to describe an algorithm.
I can interpret, correct, refine and complete a range of algorithms using algorithms, flowcharts and the OCR reference language.
My revision focus: