0% found this document useful (0 votes)
3 views

01 - 1 Algorithms Notes

The document provides comprehensive notes on algorithms, specifically focusing on general algorithms, sorting algorithms, and searching algorithms, with a detailed explanation of the bubble sort algorithm. It emphasizes the importance of following algorithms precisely, using flow charts for clarity, and understanding the mechanics behind sorting and searching processes. Additionally, it includes examiner tips and worked examples to aid in understanding and applying these concepts effectively.

Uploaded by

Jessie Chai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01 - 1 Algorithms Notes

The document provides comprehensive notes on algorithms, specifically focusing on general algorithms, sorting algorithms, and searching algorithms, with a detailed explanation of the bubble sort algorithm. It emphasizes the importance of following algorithms precisely, using flow charts for clarity, and understanding the mechanics behind sorting and searching processes. Additionally, it includes examiner tips and worked examples to aid in understanding and applying these concepts effectively.

Uploaded by

Jessie Chai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Head to www.savemyexams.

com for more awesome resources

Edexcel International A Level Your notes


Maths: Decision 1
Algorithms
Contents
General Algorithms
Sorting Algorithms
Searching Algorithms
Bin Packing Algorithms

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

What does an algorithm look like?


Algorithms may be presented in a variety of ways
A list of instructions, in order, written in words
Flow charts are a visual way of presenting the steps of an algorithm
These clearly show the order of instructions and any parts of an algorithm that may need
repetition

What else do I need to know about algorithms?


Some algorithms do not always provide an optimal (best) solution
However, they will give a solution that is sufficient for the purpose
Modern real-life situations can be complex to analyse
A compromise between the efficiency of an algorithm and its accuracy is often required
E.g. An algorithm may produce a 'shortest route' result that is inaccurate by 200 miles
For a very long journey, the time saved using this algorithm may be more important than this
inaccuracy

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

Examiner Tips and Tricks


You must show that you have followed an algorithm precisely
Use checks built into the algorithm to know that an algorithm is complete
State (or 'print') any 'output' at the end if that is what an algorithm instruction requires
Do not assume the output is the last number in your working

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

What do the different shaped boxes in a flow chart mean?


Each command in a flow chart is written inside a shape
The shape will depend on the type of command
Ovals are used to represent the start and the end
Rectangles are used to represent instructions
Diamonds are used to represent questions

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

How do I work with a flow chart?


Following a flow chart is generally straightforward but make sure you follow the instructions carefully
Commands given in a flow chart are intuitive to follow
Examples include
'Input ...' - The value(s)/data that the algorithm needs to be told
'Let ...' - Assign or update a value for a variable
'If ...' - A conditional statement, the outcome of which will lead to different parts of the
algorithm
'Output ...' or 'Print ...' - Write down the answer
Write down any values when instructed to
This will often involve completing a table of values
Sometimes there is a specific instruction to 'output' the final answer
Make sure the answer is separate to the table
Some questions may ask for a description of what the flow chart/algorithm produces

Examiner Tips and Tricks


If a question provides a table that is to be filled in with values that change, bear in mind that
some values may not change
In such instances, only the first entry for a value may end up being entered
Not every cell in every row/column will necessarily need completing
Follow the instructions from the flow chart ('robot mode'!)

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

a) Describe what the algorithm achieves.


The final instructions often give an idea of the purpose of an algorithm

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

The next instruction is the first question, is p > n ; the answer is no


For the answer no, the flow chart directs us to another question, is p = n ; the answer is no and so we
can complete the top row of the table

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

Output: 23 is not a square number

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

How does the bubble sort algorithm work?


Bubble sort works by comparing pairs of items on the working list
Comparisons are made working from left to right
The first item is compared to the second, the second to the third, and so on
A swap occurs if a pair of items are not in order
A pair of equal items does not count as a swap

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

Bubble sort, for n items, is complete when either

a pass involves no swaps, or

after the (n − 1) th pass

I.e. when only one item would remain on the working list

What is the working list?


The working list is the list of unordered items that a pass of the bubble sort algorithm will apply to
For the first pass every item on the list is in the working list
After the first pass, the final item is in the correct place
This item is removed from the working list
You may 'see' that other items are in the correct places
However, the algorithm ensures the highest (or smallest) item is at the end of the list
The working list is reduced by one after each pass

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

The second pass will have n − 1 items on the working list

(If required) the (n − 1) th pass will have 2 items on the working list

The ordered items may be described as the sorted list


The image below shows a list at the end of the second pass of an ascending bubble sort
Two items are on the sorted list (12 and 15)
The 10 being in the correct position is irrelevant at this stage
The third pass will confirm the position of the 10 and it will become part of the sorted list

How do I work out the (maximum) number of passes in the


bubble sort algorithm?
For a list of n items

The maximum number of passes will be n − 1

After the (n − 1) th pass, n − 1 items will be in order

The last remaining item must be in order too


It is hard to predict how many passes the bubble sort will need
It may be possible to tell how many more passes are required when only a few items are out of
order

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

How do I work out the (maximum) number of comparisons in


the bubble sort algorithm? Your notes
For a list of n items

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 second pass will have n − 2 comparisons

The k th pass will have n − k comparisons

(If required) the (n − 1) th pass will have 1 comparison

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

How do I work out the (maximum) number of swaps in the


bubble sort algorithm?
For a list of n items

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

Examiner Tips and Tricks Your notes


Make it clear when you have completed the bubble sort algorithm
State how you know it is complete
E.g. "There were no swaps in the fifth pass so the algorithm is complete and the list is in
order"
After completing your solution, check again if the list should be in ascending or descending
order
If you've got it the wrong way round, there's no need to redo the question but
Make it clear that after the algorithm is completed, you are reversing the list
Make sure your final answer is in the order required by the question

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.

i) The maximum number of passes is n − 1

n =7
7−1=6
The maximum number of passes will be 6

ii) The k th pass will have n − k comparisons

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

The first swap will be the 7 and 4


The 9, being the highest value on the list, will then swap with each of the values that follow it (3 values) Your notes
The number of swaps in the first pass is 4
b) After the second pass of the bubble sort algorithm, the times are in the order 4, 5, 3, 5, 6, 7, 9.
Perform the third pass of the bubble sort algorithm and state the number of swaps made.
As this is the third pass, the last two items on the list, 7 and 9, will already be in the correct place
The working list (that we apply a pass of the bubble sort algorithm to) will be 4, 5, 3, 5, 6

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

Which item is the pivot in the quick sort algorithm?


The pivot is the middle value in a sub-list of the items, without reordering

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

How do I apply the quick sort algorithm?

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

Examiner Tips and Tricks


Make it clear which items you have chosen as pivots at each pass of the quick sort algorithm
Use different styles to highlight the pivots such as
underlining values using solid, dotted, wavy, double lines etc.
drawing different shaped boxes (e.g. rectangle, circle, star) around values
Do not use different colours on the exam paper as they may not show up on the examiner's
scanned copy

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

How does the binary search algorithm work?


The binary search algorithm works by finding a pivot that splits an ordered sub-list into two halves
In the first search, the sub-list is the complete original list
The item being searched for is compared to the pivot
If the item is the same as the pivot then the item has been found
If the item comes before the pivot, the search continues in the sub-list before the pivot
If the item comes after the pivot, the search continues in the sub-list after the pivot
The algorithm is complete when
the item has been located or
the item is identified as not being in the list

Which item is the pivot in the binary search algorithm?


The pivot is the value in the middle of a sub-list of items
To find the position of the pivot
Add the positions of the first and last items in the sub-list
Divide the value by 2
Round up to the next integer value if necessary

Examiner Tips and Tricks

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

Compare 'Jason' to the pivot


Jason comes before Lorcan
Reject items 5 to 10
Write down the reduced list

1. Becky
2. Jason
3. Juan
4. Kia

Find the pivot of the reduced list

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

Compare 'Jason' to the pivot


Jason comes before Juan
Reject items 3 to 4
Write down the reduced list

1. Becky
2. Jason

Find the pivot of the reduced list

(1 + 2)
= 1 . 5 → 2 nd item
2

2. Jason

The name has been found so write a final statement


The search is complete
Jason has been found

b) Michelle
Find the pivot

(1 + 8)
= 4 . 5 → 5 th item
2

5. Lorcan

Compare 'Michelle' to the pivot


Michelle comes after Lorcan
Reject items 1 to 5
Write down the reduced list

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

Find the pivot of the reduced list

(6 + 8)
= 7 th item
2

7. Samira

Compare Michelle to the pivot


Michelle comes before Samira
Reject items 7 to 8
Write down the reduced list
The list has been reduced to one name, which is not Michelle

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

Bin Packing Algorithms


Your notes
Introduction to Bin Packing Algorithms
Bin packing algorithms organise objects into as few containers as possible
The containers are called bins
In problems all bins will be of equal size
Size may refer to length, width, capacity (volume), weight, etc.
'Packing' is a loose term that could mean 'separating', 'cutting' or similar
Examples of bin packing problems include:
Loading pallets (objects) of varying weights into lorries (bins) without exceeding their weight
capacity
The aim is to minimise the number of lorries required
Cutting short strips of cable (objects) from large reels of cable (bins)
The aim is to minimise wastage at the end of a reel
There are three bin packing algorithms covered
The first-fit bin packing algorithm
The first-fit decreasing bin packing algorithm
The full-bin packing algorithm
All are heuristic algorithms - they will provide a (good) solution
but not necessarily an optimal solution
and not necessarily the only (good) solution

First-Fit Bin Packing Algorithm


What is the first-fit bin packing algorithm?
The first-fit bin packing algorithm packs objects into bins in the order in which they are presented
E.g. Scattered boxes are organised in the order they are encountered
They are not organised beforehand

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

How do I apply the first-fit bin packing algorithm?


Each object, in turn, is placed in the first available bin that has enough capacity (room) for it
Object 1 (the first on the list) will be placed into bin 1
If bin 1 then has enough spare capacity, object 2 (the second on the list) will also go into bin 1
If not, object 2 will need a new bin, bin 2
Object 3 would be considered for bin 1 first, then bin 2, and failing both, it would require a new
bin, bin 3
For each object, if an existing bin does not have enough capacity remaining, a new bin will be used
For example,
If a bin has capacity 10 and the first two objects are of capacity 6 and 3, the 6 will go into bin 1,
then the 3 will go into bin 1 too
(6 + 3 = 9 ≤ 10)
If the first two objects are of capacity 5 and 9, the 5 would go in bin 1, then the 9 would go into
bin 2
(5 + 9 = 14 > 10)
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 bin packing problem is the smallest
integer that satisfies

Total capacity of all objects


LB ≥
Size of each bin
As it is a number of bins, the lower bound will be an integer

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

The calculation should always be rounded up to the nearest integer


E.g. For a value of 3.01, more than 3 bins would be required and so the lower bound will be 4 Your notes
Note that this is exactly the same lower bound as for the first-fit decreasing and the full-bin packing
algorithms

Examiner Tips and Tricks


As you place values into bins, you may find it helpful to jot down next to each bin
either the total capacity used in the bin
or the remaining capacity in the bin
However, do not include these as part of your final answer

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]

The second weight, 16 kg will also fit on the first shelf

Shelf 1: 12 16 [12]

Next, the 18 kg will need to go on to a new shelf (shelf 2)

Shelf 1: 12 16 [12]

Shelf 2: 18 [22]

The 8 kg weight will go on the first shelf

Shelf 1: 12 16 8 [4]

Shelf 2: 18 [22]

Continuing in this manner, the 16 kg weight can go on shelf 2


The second 12 kg weight will need a third shelf

Shelf 1: 12 16 8 [4]

Shelf 2: 18 16 [6]

Shelf 3: 12 [28]

A fourth shelf is needed for the third 12 kg weight


The 20 kg and 24 kg weights require a shelf of their own
When finished, double check each shelf does not exceed 40 kg in total
Do not include the spare capacities with the final answer

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 3: 12 10 10 Your notes

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

First-Fit Decreasing Bin Packing Algorithm


What is the first-fit decreasing bin packing algorithm?
The first-fit decreasing bin packing algorithm packs objects into bins once they have been arranged
into decreasing (descending) order
E.g. Scattered boxes are arranged in descending order of size/weight, starting with the
biggest/heaviest
The algorithm finds the number of bins required to pack all the objects
Ideally this would be the minimum number of bins required
However, the algorithm will not necessarily provide the optimal solution
An advantage of using the first-fit decreasing bin packing algorithm is that it usually gets close to the
optimal solution
This is good for situations where minimising the number of bins is more desirable than speed

How do I apply the first-fit decreasing bin packing algorithm?


Each object, in turn, is placed in the first available bin that has enough capacity (room) for it
Object 1 (the biggest) will be placed into bin 1
If bin 1 then has enough spare capacity, object 2 (the second biggest) will also go into bin 1
If not, object 2 will need a new bin, bin 2
Object 3 would be considered for bin 1 first, then bin 2, and failing both, it would require a new
bin, bin 3
For each object, if any existing bin does not have enough capacity remaining, a new bin will be
required

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

Total capacity of all objects


LB ≥
Size of each bin
As it is a number of bins, the lower bound will be an integer
The calculation should always be rounded up to the nearest integer
E.g. For a value 3.01, more than 3 bins would be required and so the lower bound will be 4
Note that this is exactly the same lower bound as for the first-fit and full-bin packing algorithms

Examiner Tips and Tricks


A question may have a previous part that asks you to arrange values into descending order
You may be asked to name a suitable algorithm that would sort values in this way
Suitable algorithms for this would be 'bubble sort' or 'quick sort'
You may be asked to briefly explain these algorithms

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 2 has enough capacity to take the next, 18 kg weight

Shelf 1: 24 [16]

Shelf 2: 20 18 [2]

The first of the 16 kg weights can fill shelf 1

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]

A fourth shelf is needed for the three 12 kg weights (leaving 4 kg spare)


So a fifth shelf is needed for the three 10 kg weights (leaving 10 kg spare)

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

Full-Bin Packing Algorithm


What is the full-bin packing algorithm?
The full-bin packing algorithm identifies combinations of objects that will fill a bin
Any objects that cannot be grouped to fill a bin will be packed using the first-fit bin packing
algorithm
This algorithm finds the number of bins required to pack all the objects
Ideally this would be the minimum number of bins required
An advantage of the full-bin packing algorithm is that it generates a good solution
It will often produce the optimal solution
Full-bin combinations are identified by inspection
This can be time-consuming for a situation with many objects
It is not suitable for situations where speed is important

How do I apply the full-bin packing algorithm?


The list of objects is inspected to identify combinations of objects that fill a bin
As many combinations of full-bins as possible from the original list should be identified and
placed into bins

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

E.g. For the list of objects: 2, 6, 4, 9, 8, 8, 2, 5, 2 and a bin capacity of 10


A possible set of full-bin combinations is: 2 + 8, 6 + 4, 8 + 2 Your notes
The remaining objects are 9, 5 and 2
Any remaining objects should be placed into bins using the first-fit bin packing algorithm
E.g. for the remaining objects above
9 will be place in the next available bin
5 and 2 will be placed in an additional bin
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 full-bin packing problem is the smallest
integer that satisfies

Total capacity of all objects


LB ≥
Size of each bin
As it is a number of bins, the lower bound will be an integer
The calculation should always be rounded up to the nearest integer
E.g. For a value 3.01, more than 3 bins would be required and so the lower bound will be 4
Note that this is exactly the same lower bound as for the first-fit and the first-fit decreasing bin packing
algorithms

Examiner Tips and Tricks


As this algorithm is mostly done by inspection, be very careful as you find the full-bin
combinations
Check off each item as it is included in a bin
Check that you have the same number of objects in your bins as there are in the original list

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

The lower bound is 5 shelves so the solution is optimal


There are other possible 5-shelf solutions using the full-bin algorithm

Page 31 of 31
© 2015-2025 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers

You might also like