01 - 1 Algorithms Notes
01 - 1 Algorithms Notes
Page 1 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
General Algorithms
Your notes
Introduction to Algorithms
What is an algorithm?
An algorithm is a set of precise instructions that, if strictly followed, will result in the solution to a
problem
Algorithms are particularly useful for programming computers
Computers can process huge amounts of data and perform millions of calculations in very
short time frames
Robots can be programmed to follow an algorithm in order to complete a task
E.g. A robot vacuum cleaner or lawn mower
Algorithms can be performed by human beings too!
E.g. The recipe and cooking instructions for a cake
Page 2 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
When using an algorithm with a small amount of data in an exam you must follow the algorithm
precisely and accurately
Your notes
This demonstrates understanding of the algorithm
Using common sense or intuition to 'see' the solution will not gain full marks
Flow Charts
What is a flow chart?
A flow chart is a diagrammatic way of listing the instructions to an algorithm
Flow charts lend themselves to algorithms that have
conditional instructions
E.g. Is a > b ?
If the answer is yes, the flow chart directs the user to one instruction
If the answer is no, the flow chart directs the user to a different instruction
repetitive parts or stages
Page 3 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 4 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Worked Example
An algorithm is presented as a flow chart, shown below. Your notes
The algorithm works out if the input value n is a square number or not
b) For the input n = 23 , use the flow chart to perform the algorithm.
Complete the table.
n k p Is p > n ? Is p = n ?
Output:
Page 5 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
The first instruction is to input n , so fill 23 under n in the first row of the table
The next two instructions are to let k = 1 and to let p = k 2 , so also in the first row we can fill in 1 and
Your notes
1 for k and p
n k p Is p > n ? Is p = n ?
23 1 1
n k p Is p > n ? Is p = n ?
23 1 1 NO NO
The flow chart now updates the value of k ; indicate this on the second row of the table
The flow chart then takes us back round the loop of finding and testing the value of p
n does not get updated so only appears in the first row of the table
n k p Is p > n ? Is p = n ?
23 1 1 NO NO
2 4 NO NO
The table can be completed by continuing to precisely follow the flow chart
The output needs stating at the end as per the flow chart instructions
Note how the question "is p = n ?" does not get considered as the algorithm outputs and stops
following the answer 'yes' to the question "is p > n ?"
n k p Is p > n ? Is p = n ?
Page 6 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
23 1 1 NO NO
2 4 NO NO Your notes
3 9 NO NO
4 16 NO NO
5 25 YES
Text-Based Algorithms
What are text-based algorithms?
Text-based algorithms refer to instructions that are given in sentences
Each instruction may be labelled with 1, 2, 3, and so on
The following text-based algorithm is for a very basic single player dice game
The aim of the game is to minimise the number of rolls of the dice needed to reach 'Stop'
Page 7 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Sorting Algorithms
Your notes
Introduction to Sorting Algorithms
Sorting algorithms arrange items into ascending or descending order
Items that are usually sorted include values, letters or words
As a list of items becomes larger, it becomes increasingly difficult for a human to sort
A computer can be programmed with a sorting algorithm
This makes the process both accurate and efficient
With sorting algorithms in particular, it is important to stay in 'robot mode'
Follow each step of the algorithm precisely, in order, exactly as a robot would
Do not be tempted to take shortcuts or miss parts of the algorithm out because the answer can be
'seen'
Bubble Sort
What is the bubble sort algorithm?
The bubble sort algorithm arranges items into either ascending or descending order
Items are usually values
They could also be letters or words or similar
For questions given in context, values will be measures such as weights, lengths, scores or
times
Page 8 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
This comparison of pairs and swaps continues until the end of the working list is reached
This is called a pass
Several passes are usually required to order a list of items
At the end of each pass, the item at the end of the working list is in the correct place
For ascending order, the highest value 'bubbles' to the end
I.e. when only one item would remain on the working list
Page 9 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
This keeps the bubble sort as efficient as possible by not requiring unnecessary comparisons
n
For a list of items
Your notes
The first pass will have n items on the working list
(If required) the (n − 1) th pass will have 2 items on the working list
If the item at the end of the list should be at the start then it will take n − 1 passes
This is because this item will only move one space closer to the start after each pass
Page 10 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
The number of comparisons at each pass will always be one less than the number of items on the
working list
The first pass will have n items on the working list so there will be n − 1 comparisons
The maximum number of comparisons for the entire bubble sort algorithm would be
(n − 1) + (n − 2) + (n − 3) + . . . + 3 + 2 + 1
or
n −1
1
∑r= 2
n (n − 1)
r =1
Understanding the logic for the number of comparisons is easier than trying to remember a formula
The maximum number of swaps in a pass would be the same as the number of comparisons for
that pass
I.e. if every comparison results in a swap being made
This would occur if the first item on the working list is the largest (or smallest for descending)
With a small working list it is possible to 'see' or count the number of swaps required for a pass
For a longer list, keep a tally of swaps as you perform a pass
The maximum number of swaps for the entire algorithm would be needed if a list started in
reverse order
Page 11 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Worked Example
The times, to the nearest second, taken by 7 participants to answer a general knowledge question
are 5, 7, 4, 9, 3, 5, 6.
The times are to be sorted into ascending order using the bubble sort algorithm.
a) Write down
i) the maximum number of passes the algorithm will require,
ii) the number of comparisons required for the third pass,
iii) the number of swaps there will be in the first pass.
n =7
7−1=6
The maximum number of passes will be 6
7−3=4
The number of comparisons required in the third pass will be 4
iii) Carefully do this by inspection (or you can carry out the first pass to be sure but that takes time)
Page 12 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
4 5 3 5 6 7 9 4 < 5, no swap
4 5 3 5 6 7 9 5 > 3, swap
4 3 5 5 6 7 9 5 = 5, no swap
4 3 5 5 6 7 9 5 < 6, no swap
4 3 5 5 6 7 9 end of pass 3
Number of swaps: 1
c) Explain why two further passes of the bubble sort algorithm are required to ensure the list is in
ascending order.
At this stage of the algorithm you should be able to deduce the answer by inspection of the current
list
Using the answer to part (b), the current list is 4, 3, 5, 5, 6, 7, 9
The next pass will involve one swap - the 4 and the 3
The pass after that will involve no swaps and so the algorithm will be complete and the list will be
in order
Thus, two further passes are required
It is important to state that the algorithm is complete and the reason why
Quick Sort
What is the quick sort algorithm?
The quick sort algorithm arranges items into either ascending or descending order
Items are usually values
They could be letters or words or similar
Page 13 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
For questions given in context values will be measures such as weights, lengths, scores or
times
Your notes
How does the quick sort algorithm work?
Each pass of the quick sort algorithm works by splitting a sub-list of the items into two halves around a
pivot
One half will be the sub-list of values that are less than the pivot
For ascending order this sub-list would come before the pivot
The other half will be the sub-list of values that are greater than the pivot
For ascending order this sub-list would come after the pivot
Values that are equal to the pivot can be listed in either half
But for consistency always list items equal to the pivot in the half greater than the pivot
On both sides of the pivot, values should be listed in the order they appear in the original list
The algorithm is complete when every item on the (original) list has been designated as a pivot
n +1
For a list containing n items, the pivot will be the th item
2
For consistency round up if necessary
In the first pass, there will be one pivot
This is the middle item in the whole list
In the second pass, there could be two (new) pivots
One pivot in the lower sub-list and one in the upper sub-list
The number of pivots could double at each pass
But this will not always be the case
If a pivot is the lowest or greatest item on a sub-list
Page 14 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
The following list of steps describes the quick sort algorithm for arranging a list of items into
ascending order
Your notes
Arranging into descending order would be the same
However, the 'ordering' in Step 2 would be reversed
STEP 1
Find and highlight the pivot (middle value) for each sub-list of items
For the first pass only, the sub-list will be the same as the entire list of items
STEP 2
List items lower than the pivot, in the order they appear, before the pivot
Then write the pivot as the next number on the list
List items greater than or equal to the pivot, in the order they appear, after the pivot
STEP 3
Repeat steps 1 and 2 until every item on the original list has been designated a pivot
The original (full) list of items will now be in ascending order
Worked Example
The following values are to be sorted into descending order using the quick sort algorithm.
5, 3, 7, 2, 4, 5, 9, 6
Clearly showing your choice of pivots at each pass, perform the quick sort algorithm to sort the list
into descending order.
Page 15 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
STEP 1
This is the first pass so the entire list will be the sub-list
Your notes
8+1
There are 8 values so the pivot will be the = 4 . 5 rounded up to the 5th value on the list
2
Pass 1 (pivot 4)
5 3 7 2 5 9 6
4
STEP 2
As descending order is required, list items greater than the pivot (4) before it; items lower than
the pivot after it
Keep the numbers in the order they are already in
5 7 5 9 6 3 2
4
STEP 3
There are now two sub-lists 5, 7, 5, 9, 6 and 3, 2
Repeat steps 1 and 2 on these sub-lists
Pass 2 (pivots 5 and 2)
5 7 5 9 6 3 2
4
The (repeated) 5 will go into the 'greater than or equal to' sub-list (before the pivot 5 as descending)
There will be no new sub-lists at the next pass after the pivot 5 or pivot 2 as they are the lowest values
in their current sub-lists
5 7 9 6 5 3 2
4
Continuing repeating steps 1 and 2 on each new sub-list until every item has been designated a pivot
Pass 3 (pivots 9 and 3)
5 7 9 6 5 3 2
4
9 5 7 6 5 3 2
4
Pass 4 (pivot 7)
9 5 7 6 5 3 2
4
Page 16 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
9 7 5 6 5 3 2
4
Your notes
Pass 5 (pivot 6)
9 7 5 6 5 3 2
4
9 7 6 5 5 3 2
4
Pass 6 (pivot 5)
9 7 6
⎡⎢ 5 ⎤⎥
⎣ ⎦ 5 4 3 2
Every value has now been designated as a pivot so the algorithm is complete and the list is in
descending order
9, 7, 6, 5, 5, 4, 3, 2
Page 17 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Searching Algorithms
Your notes
Binary Search
What is the binary search algorithm?
The binary search algorithm inspects an ordered list to determine if a specified item is in the list
If the item is in the list, the search algorithm will find and locate the item
Page 18 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
If the items in the list are not numbered, it is a good idea to number them in ascending order as a
first step
Make a clear statement at the end of the algorithm to show that it is complete Your notes
If the item has been found: 'The search is complete, ..... has been found in the list'
If the item has not been found: '..... is not in the list'
Worked Example
Below is a list of names.
1. Becky
2. Jason
3. Juan
4. Kia
5. Lorcan
6. Ryan
7. Samira
8. Selina
Use the binary search algorithm to find the following names in the list.
a) Jason
Find the pivot
(1 + 8)
= 4 . 5 → 5 th item
2
5. Lorcan
1. Becky
2. Jason
3. Juan
4. Kia
Page 19 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
(1 + 4)
= 2 . 5 → 3 rd item
2 Your notes
3. Juan
1. Becky
2. Jason
(1 + 2)
= 1 . 5 → 2 nd item
2
2. Jason
b) Michelle
Find the pivot
(1 + 8)
= 4 . 5 → 5 th item
2
5. Lorcan
Page 20 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
6. Ryan
7. Samira
Your notes
8. Selina
(6 + 8)
= 7 th item
2
7. Samira
6. Ryan
Reject item 6
Write a final statement
Michelle is not in the list
Page 21 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 22 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
The algorithm finds the number of bins required to pack all the objects
Ideally this would be the minimum number of bins required Your notes
However, the algorithm will not necessarily provide the optimal solution
An advantage of using the first-fit bin packing algorithm is speed
The objects do not have to be sorted (into size or any other criteria) first
In many cases, the solution obtained, will be sufficient for business or practical purposes
I.e. speed (efficiency) is more important than optimisation
Page 23 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Worked Example
A gym worker is tidying up by stacking weights on shelves. The weights, given in kilograms, are
stated below.
12, 16, 18, 8, 16, 12, 10, 4, 6, 10, 12, 16, 20, 10, 24
Each shelf has a load capacity of 40 kg.
a) Find a lower bound for the number of shelves needed so all the weights can be stacked on the
shelves.
The lower bound will be (greater than or equal to) the total of the weights divided by the capacity
(load) of one shelf
12 + 16 + 18 + 8 + 16 + 12 + 10 + 4 + 6 + 10 + 12 + 16 + 20 + 10 + 24
LB ≥
40
194
LB ≥
40
LB ≥ 4 . 85
The number of shelves (bins) will need to be an integer (hence the inequality)
More than four shelves are needed so round up to five
Lower bound for the number of shelves is 5
Page 24 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
b) Use the first-fit bin packing algorithm to find the number of shelves needed to stack all of the
weights.
Your notes
In this case, a 'shelf' will be a 'bin'
In the first-fit bin packing algorithm, we deal with each weight in turn as they are listed
(The remaining capacity, written in brackets next to each shelf in the working area, is optional)
The first weight, 12 kg, will go on the first shelf
Shelf 1: 12 [28]
Shelf 1: 12 16 [12]
Shelf 1: 12 16 [12]
Shelf 2: 18 [22]
Shelf 1: 12 16 8 [4]
Shelf 2: 18 [22]
Shelf 1: 12 16 8 [4]
Shelf 2: 18 16 [6]
Shelf 3: 12 [28]
Shelf 1: 12 16 8 4
Page 25 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Shelf 2: 18 16 6
Shelf 4: 12 16 10
Shelf 5: 20
Shelf 6: 24
Notice that, whilst the lower bound was 5 shelves, the algorithm used 6 shelves
Page 26 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
For example,
If a bin has capacity 15 and the first two objects are of capacity 13 and 10, the 13 will go into bin Your notes
1, then the 10 will go into bin 2
(as 13 + 10 = 23 > 15)
If the first two objects are of capacity 8 and 6, the 8 would go in bin 1, then the 6 would go into
bin 1 too
(as 8 + 6 = 14 ≤ 15)
The algorithm is complete when all objects have been placed in a bin
How do I find the lower bound for the number of bins?
The lower bound (LB) for the number of bins required in the first-fit decreasing bin packing problem is
the smallest integer that satisfies
Worked Example
A gym worker is tidying up by stacking weights on shelves. A list of the weights at the gym, given in
kilograms, in descending order is stated below.
24, 20, 18, 16, 16, 16, 12, 12, 12, 10, 10, 10, 8, 6, 4
Page 27 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Each shelf has a load capacity of 40 kg. The lower bound for the number of shelves required to stack
all of the weights is 5.
Your notes
Use the first-fit decreasing bin packing algorithm to stack the weights onto as few shelves as
possible, explaining how you know your answer is optimal.
A 'shelf' will be a 'bin'
For the first-fit decreasing bin packing algorithm, assign the weights starting with the heaviest, in
descending order
The first weight, 24 kg, will go on the first shelf
(The remaining capacity, in brackets next to each shelf in the working area, is optional)
Shelf 1: 24 [16]
The second weight, 20 kg will not fit on the first shelf so a new shelf is needed
Shelf 1: 24 [16]
Shelf 2: 20 [20]
Shelf 1: 24 [16]
Shelf 2: 20 18 [2]
Shelf 1: 24 16 [0]
Shelf 2: 20 18 [2]
Thinking ahead we can now see that the lowest weight is 4 kg, but there is only 2 kg of spare capacity
in the first two shelves
Therefore we know we can no longer use shelves 1 or 2
The other two 16 kg weights can both go on to shelf 3
Shelf 1: 24 16 [0]
Shelf 2: 20 18 [2]
Shelf 3: 16 16 [8]
Page 28 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
There is then spare capacity on shelves 3, 5 and 4 respectively for the 8 kg, 6 kg and 4 kg weights
Double check your final answer and do not include the spare capacities
Your notes
Shelf 1: 24 16
Shelf 2: 20 18
Shelf 3: 16 16 8
Shelf 4: 12 12 12 4
Shelf 5: 10 10 10 6
This uses the same number of bins as the lower bound stated in the question
This solution is optimal as the number of bins required is the same as the lower bound given
Other arrangements on 5 shelves may be possible
Page 29 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Worked Example
Page 30 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
A gym worker is tidying up by stacking weights on shelves. The weights, given in kilograms, are
stated below.
Your notes
12, 16, 18, 8, 16, 12, 10, 4, 6, 10, 12, 16, 20, 10, 24
Each shelf has a load capacity of 40 kg.
Use the full-bin packing algorithm to find the number of shelves needed to stack all of the weights.
In this case, a 'shelf' will be a 'bin'
In the full-bin packing algorithm, we inspect the original list for combinations equal to the capacity
of each bin
Find combinations of weights that add to 40 and place on shelves
Shelf 1: 12 12 16
Shelf 2: 8 10 10 12
Shelf 3: 16 24
Shelf 4: 16 20 4
Sort the remaining objects in the order they appear using the first-fit algorithm
The remaining objects, in order, are 18, 6 and 10
These will all fit in a single bin
Shelf 1: 12 12 16
Shelf 2: 8 10 10 12
Shelf 3: 16 24
Shelf 4: 16 20 4
Shelf 5: 18 6 10
Page 31 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers