Professional Documents
Culture Documents
Mahmud RAHMAN - 2.1 Workbook
Mahmud RAHMAN - 2.1 Workbook
1 | Algorithms Craig’n’Dave
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:
Be able to fly
Stay in the air for a few seconds
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:
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.
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
Red beads
Chain
Purple beads
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
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
An input is: Data entered by the user. It has a certain data type.
An output is: Data produced by performing operations on the input. It has a certain data type.
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
Program:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Algorithmic thinking
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.
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
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
82
43
27
38
Merge sort
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
24
Yellow dotted box:
unsorted data in the list:
16
32
Line
Decision
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
1. Play game
2. Change character
3. Quit
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
Feedback
Breadth Depth Understanding
□ Some aspects complete □ Basic level of depth shown □ Some work is accurate
□ Little work complete □ Little depth and detail provided □ Little work is accurate