Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

GCSE J277 Unit 2.

1 | Algorithms Craig’n’Dave

Name: Mahmud Rahman

Specification & learning objectives


By the end of this topic you will have studied:
• Principles of computational thinking: Abstraction, Decomposition, Algorithmic thinking
• Identify the inputs, processes, and outputs for a problem
• Structure diagrams
• Create, interpret, correct, and refine algorithms using: Pseudocode, Flowcharts, Reference language/high-level programming language
• Identify 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: Simplifying a program by cutting it down and removing unnecessary features

Example of an abstraction:

Real aeroplane: Paper aeroplane: Necessary features of a paper aeroplane:

Be able to fly
Stay in the air for a few seconds

Unnecessary features of a paper aeroplane:

Carry passengers
Carry cargo
Fly for several hours
Have a landing gear
Land on wheels
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Abstraction
Cat: Cat icon: Necessary features of the icon: Unnecessary features of the icon:

Represent a cat Have the eyes, face and mouth of a cat


Be in the shape of a cat Have well-defined features
Show a cat’s tail

Dog: Dog icon: Necessary features of the icon: Unnecessary features of the icon:

Represent a dog
Be in the shape of a dog
Show a dog’s tail

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 that outputs whether a capital city in Europe is north or south of another capital city in Europe 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:
city = input(“Please enter the name of the
city: “)
latitude = float(input(“Please enter the
latitude of the city: “))
if latitude > 51.5074:
print(“City is north of London.”)
elif latitude < 51.5074:
print(“City is south of London.”)
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Decomposition

Decomposition means: Breaking down a problem into several subproblems in order to make it easier to solve

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 making 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 “Manage pupils details” program:


GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Identify the input, processes and outputs for a problem

An input is: Data entered by the user. It has a certain data type.

A process is: What operations are being performed on the input.

An output is: Data produced by performing operations on the input. It has a certain data type.

Title of program What does it do? Inputs Processes Outputs

Swimming pool volume Calculates the total width : real Volume = width * length * volume : real
volume of a swimming length : real depth
pool 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: A search algorithm which works by iterating through each item in a list until the desired item is found.

Steps to find the Geography Zeroth item: Archaelogy


book on the shelf using a It is not Geography, so move on
linear search: First Item: Art
It is not Geography, so move on
Second item: Biology
It is not Geography, so move on
Third item: Chemistry
It is not Geography, so move on

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 performed by the: algorithm. However, this is not true if the first item in the list is the one you want to find.

If the item you want to find is first in the list then 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 24 32

8 8 24

16 16 16

2 32 8

32 2 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 are


lists together. 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, 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 in the list:

16

32

Green solid box:


sorted data in the list:

? inserted ? inserted ? inserted ? inserted ? inserted


in place. in place. in place. in place. in place.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Flow diagram symbols

Terminal Start/end Input/Output

Process Sub Routine

Line
Decision
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

How to produce algorithms using flow diagrams

An algorithm for an RPG game print(“You have


displays 3 choices from a menu and
allows the user to enter their choice.

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 an RPG game do


displays 3 choices from a menu and print("1. Play game")
allows the user to enter their choice. print("2. Change character")
print("3. Quit")
1. Play game
2. Change character input(int(choice))
3. Quit
until choice<1 AND choice>4
The user input is validated so only
the numbers 1-3 can be entered.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

How to produce algorithms using flow diagrams

An algorithm for an RPG 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 dice
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 an RPG 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 dice
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 an RPG 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 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 flow diagrams

An RPG game allows a player to input


their next move by entering N, E, S
or W. The 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.

Assuming a zero indexed list/array.


GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

How to produce algorithms using pseudocode (OCR reference language)

An RPG game allows a player to input


their next move by entering N, E, S
or W. The 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 in this is on line: print(Player 1 wins)

The type of error is: Syntax error

In order to fix this error:


Insert quotation marks before the first and after the last word of the string.
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 in this is on line:

The type of error is:

In order 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 in this is on line:

The type of error is:

In order to fix this error:


GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Trace tables

Assuming that the when this program is run the user enters the values…
01: x = int(input("Enter a number: "))
• x = 2 in line one
02: y = int(input("Enter another number: "))
• y = 7 in line two
03:
…complete trace table for this program is as follows:
04: total = x
05:
06: for counter = 1 to y
X=2 Y=7 total counter 07: total = total * x
2 7 2 1 08: print(total)
09: next counter
2 7 4 2
2 7 8 3
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Assessment Target: Overall grade:

Minimum expectations by the end of this unit


□ You should have learnt terms 134-151 from your GCSE Level Key Terminology during this unit.
□ You have completed all the 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 & action Student response


GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave

Reflection & Revision checklist


Confidence Clarification
☹😐☺ I can explain what is meant by the term abstraction.
☹😐☺ I can explain why abstraction is helpful when we are designing a solution to a problem.
☹😐☺ I can explain what decomposition is and how it is useful.
☹😐☺ I can explain what is meant be ‘algorithmic thinking’.
☹😐☺ I can identify inputs, processes and outputs for 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 flow diagram 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 will need to be:

You might also like