Algorithms Worksheet
Algorithms Worksheet
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.
2.
a. (i) The array people contains the values:
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".
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”)
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.
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.
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:
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
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
Merge Sort
3.
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
num = 3
for x = 1 to num
print x * num
next x
b.
num = 3
for x = 1 to num
print x * num
next x
num x Output
1 3 1 3
2 3 2 6
3 3 3 9
7. A bicycle dealer uses the following algorithm to determine the price to charge for bicycles.
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.
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