0% found this document useful (0 votes)
11 views12 pages

Algorithms Worksheet

The document covers various concepts related to algorithms, including decomposition and abstraction in problem-solving and software development. It discusses searching algorithms like binary and linear search, their efficiencies, and sorting algorithms like bubble sort and insertion sort, along with their advantages and disadvantages. Additionally, it includes pseudocode for categorizing students into house groups based on birth months and outlines a bicycle pricing algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
11 views12 pages

Algorithms Worksheet

The document covers various concepts related to algorithms, including decomposition and abstraction in problem-solving and software development. It discusses searching algorithms like binary and linear search, their efficiencies, and sorting algorithms like bubble sort and insertion sort, along with their advantages and disadvantages. Additionally, it includes pseudocode for categorizing students into house groups based on birth months and outlines a bicycle pricing algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

End of topic quiz – Topic 2.

1 Algorithms
1.
a. Give examples of how decomposition can be used when thinking
computationally.
  Breaking down a problem into smaller parts: For
instance, when creating a weather forecasting system, the
task could be decomposed into smaller tasks such as
collecting data, analyzing data, generating predictions, and
displaying results.
  Software development: Decomposing a large project
into modules or functions, like splitting a game into
modules such as graphics, gameplay, and user input
processing.
  Problem-solving: A complex algorithm could be broken
into steps like initialization, processing, and output.

b. A theme park uses a 3D computer simulation of a rollercoaster. Riders


must wear a virtual reality headset to experience the ride.

Suggest how abstraction could be used giving examples for this


simulation.

 Simplification of details: The simulation could abstract


away the exact physical mechanisms of the rollercoaster (e.g.,
how the physics of each motion is calculated), and just show the
visual effects of the ride.
 Focus on user experience: In terms of the headset,
abstraction could involve only displaying the visual elements
related to the simulation, such as the environment and the
rollercoaster itself, hiding any complexity regarding how the
environment is generated or the physics behind it. - Levels of
interaction: Abstraction could hide lower-level details, like how
the virtual reality system processes user head movements and
focuses on just the immersive experience.

2.
a. (i) The array people contains the values:

["Imogen", "Fletcher", "Kirstie", "Zoe", "Gavin"]

Why can you not use a binary search on this array?

Version 1 1 © OCR 2020


 The elements are not in any specific order, so
binary search cannot be applied directly.

a.(ii) Once the issue identified in part a.(i) has been resolved, describe the
steps that would be taken to search the array for the value “Fletcher”
using a binary search.

 Step 1: Sort the array. The sorted array would look like:
["Fletcher", "Gavin", "Imogen", "Kirstie", "Zoe"]
 Step 2: Set low index = 0, high index = 4 (length of array -
1).
 Step 3: Calculate mid index = (0 + 4) / 2 = 2. Check the
value at index 2 ("Imogen").
 Step 4: Since "Imogen" is alphabetically after "Fletcher," set
high = 1.
 Step 5: Calculate new mid index = (0 + 1) / 2 = 0. Check
the value at index 0 ("Fletcher").
 Step 6: Since "Fletcher" matches the search value, print
"Fletcher found at index 0".

Version 1 2 © OCR 2020


b. (i) The algorithm below uses a different method to search through the array
for a name.

Fill in the gaps to complete the algorithm.

array people[5]
people = ["Imogen", "Fletcher", "Kirstie", "Zoe", "Gavin"]
found = False
x = 0
searchfor = input("Enter a name to search for : ")
while found == False and x <5
if people[x] == searchfor then
found = true
print "found at position " + str(x))
endif
x = x + 1

if found == false:
print(“Not found”)

b.(ii) What is the name of this searching algorithm?

This is a linear search algorithm, where each element is checked sequentially until a
match is found or the end of the list is reached.

c. A user has a database of 100,000 people and needs to search through to find one
particular person.

Compare the use of both searching algorithms covered in parts a and b for a data set of
this size.

 Binary Search:
o Time Complexity: O (log n), which makes it much more efficient for
large datasets (100,000 people) since it reduces the problem size
significantly at each step.
o However, the array must be sorted first, which requires additional time
(O (n log n) for sorting).
 Linear Search:
o Time Complexity: O(n), which means it would check each item one by
one. For 100,000 people, this can be slower than binary search because
it may need to check all 100,000 entries.
Thus, binary search would be more efficient for large datasets but requires the
data to be sorted first.

Version 1 3 © OCR 2020


Version 1 4 © OCR 2020
3.

a.(i) A programmer has a list of numbers in an array called scores, as shown below:

17 9 4 -12 3 39

When setting up a bubble sort algorithm for these numbers, the programmer uses a
variable called swaps which can either be True or False.

What is the data type of the variable swaps?

The data type of swaps is Boolean (True or False). This variable keeps track of whether
any swaps were made during an iteration of the bubble sort.

a.(ii) Describe the use of this variable when implementing the bubble sort.

 In a bubble sort, the swaps variable is initially set to True before starting the
sorting process.
 During each iteration, if any two adjacent elements are out of order, they are
swapped, and swaps is set to True.
 If no swaps occur during a full pass, swaps is set to False, and the algorithm can
terminate early as the list is sorted.

b. One section of the bubble sort algorithm used by the programmer is shown below:

if scores[x] > scores[x + 1] //if scores in wrong order


scores[x] = scores[x+1]
scores[x+1] = scores[x] // swap numbers over
endif

What is the error that is contained in the code above? Provide a corrected version.

The issue in the code is that after swapping the elements, the second element’s value is
overwritten by the first, causing an incorrect swap. The correct version should use a
temporary variable to hold one of the values while swapping.

c. How would an insertion sort algorithm arrange the numbers in the scores array into
order?

Insertion sort works by taking each element and inserting it into its correct position

Version 1 5 © OCR 2020


relative to the previously sorted elements. It goes through the list, comparing each
element with the ones before it and shifting them until the current element is placed in
its correct position.

For the array: 17, 9, 4, -12, 3, 39 Insertion sort would go through the following
steps:
 9 would be inserted before 17
 4 would be inserted before 9
 -12 would be inserted at the beginning
 3 would be inserted before 4
 39 stays in its place Final sorted array: -12, 3, 4, 9, 17, 39

d. Name one other sorting algorithm.

 Merge Sort

e. What is one advantage and one disadvantage of using a bubble sort?

 Advantage: Simple to understand and implement.


 Disadvantage: Inefficient for large datasets due to its O(n²) time complexity.

3.

Version 1 6 © OCR 2020


4. A school divides students into house groups based on the month that they were born in. Students
born in January, February, March or April are put into Needwood house. Students born in May,
June, July or August are put into Marchington House. All other students are put into Trent house.

Using pseudocode, write an algorithm that will:

 Ask the user to enter a number (1 to 12) relating to their birth month.
 Decide which house they are in and print this out.
 Keep a running total of how many students are in each house.
 Repeat the above for 20 students.
 When 20 students have entered their details, print out how many students are in each
house.

students_in_needwood = 0
students_in_marchington = 0
students_in_trent = 0

FOR i = 1 TO 20
month = input("Enter birth month (1 to 12): ")
IF month == 1 OR month == 2 OR month == 3 OR month == 4 THEN
print "Needwood house"
students_in_needwood = students_in_needwood + 1
ELSE IF month == 5 OR month == 6 OR month == 7 OR month == 8 THEN
print "Marchington house"
students_in_marchington = students_in_marchington + 1
ELSE
print "Trent house" students_in_trent = students_in_trent + 1
ENDIF
END FOR

print "Needwood house: " + students_in_needwood


print "Marchington house: " + students_in_marchington
print "Trent house: " + students_in_trent

Version 1 7 © OCR 2020


5.

Version 1 8 © OCR 2020


5.
a.

num = 3
for x = 1 to num
print x * num
next x

Sketch a flowchart version of this algorithm.

b.

num = 3
for x = 1 to num
print x * num
next x

Complete the trace table for the program code.

num x Output
1 3 1 3
2 3 2 6
3 3 3 9

Version 1 9 © OCR 2020


Version 1 10 © OCR 2020
6. Finish the following table to describe the use of each of the following flow chart symbols.

Symbol Description of use


Start or End of a process

Process step, such as calculations or assignments

Input or Output operation

Decision or condition check

7. A bicycle dealer uses the following algorithm to determine the price to charge for bicycles.

01 p = input("purchase price of bicycle")


02 i = input("number of improvements made")
03 a = input("age of bicycle in years")
04 s = p + (i * 100)
05 if a <= 10 then
06 s = s + s
07 endif
08 print "sale price is " + s

Work out the output value with the following inputs:

a. (i) p = 1000, i = 2, a = 12

p = 1000, i = 2, a = 12
 s = 1000 + (2 * 100) = 1200
 Since a > 10, no doubling occurs. Output: 1200.

Version 1 11 © OCR 2020


a. (ii) p = 5000, i = 3, a = 10

s = 5000 + (3 * 100) = 5300

Since a = 10, the bicycle is not old enough to double the price. Output: 5300.

a. (iii) p = 8000, i = 0, a = 5

p = 800

Version 1 12 © OCR 2020

You might also like