All revision notes topics

Algorithms, bin packing and sortingEdexcel A-Level Further Maths: Revision notes

Section 1

Algorithms and flow charts

An algorithm is a finite, unambiguous sequence of steps that solves a problem. It may be given as text (numbered steps) or a flow chart, which uses boxes for input/output and instructions, diamonds for decisions (yes or no) and arrows for the flow, with start and stop terminals. To implement an algorithm, follow each step exactly, keeping a trace table of the value of each variable after every step. Example (Euclid): input a>ba>b; rr = remainder of a÷ba\div b; if r=0r=0 output bb; otherwise replace aa by bb, bb by rr and repeat. For a=252a=252, b=105b=105 the remainders are 42, 21, 0, giving output 21, the highest common factor.

Key termsalgorithmtrace table
Exam tip

Do not skip steps or work in your head. A trace table shows the method and earns the marks.

Section 2

Order of an algorithm

The size of a problem is the amount of data, such as the number of items nn. The order of an algorithm describes how the running time grows with nn for large nn: it keeps only the highest power. An algorithm of order n2n^2 takes about 100 times as long if nn is multiplied by 10. Bubble sort and first-fit bin packing are of order n2n^2; a quadratic count such as n(n−1)2\frac{n(n-1)}{2} has order n2n^2. Common orders from fastest to slowest growth: log⁡n\log n, nn, n2n^2, n3n^3, 2n2^n. The order is a guide to efficiency, not an exact time: lower-order terms and constants are ignored.

Key termsordersize of a problem
Common mistake

Saying the time doubles when nn doubles for an order n2n^2 algorithm. It is multiplied by about 4.

Section 3

Bin packing

Bin packing fits items into the fewest bins of a fixed capacity. Three algorithms: first-fit, taking items in the given order and placing each in the first bin with room; first-fit decreasing, which first sorts the items into descending order and then applies first-fit; and full-bin, which looks for combinations of items that exactly fill a bin. The lower bound on the number of bins is the total size divided by the capacity, rounded up. Example (capacity 20): items 7,13,8,3,5,9,4,14,127,13,8,3,5,9,4,14,12 total 75, so at least 4 bins. First-fit uses 5 bins: {7,13}\{7,13\}, {8,3,5,4}\{8,3,5,4\}, {9}\{9\}, {14}\{14\}, {12}\{12\}; first-fit decreasing uses 4: {14,5}\{14,5\}, {13,7}\{13,7\}, {12,8}\{12,8\}, {9,4,3}\{9,4,3\}. These are heuristics: none guarantees the optimum, except when the bound is met.

Key termsfirst-fitfirst-fit decreasingfull-binlower bound
Common mistake

Rounding the lower bound to the nearest integer. It must always be rounded up.

Section 4

Bubble sort

Bubble sort compares adjacent items from the left, swapping any pair in the wrong order. After the first pass the largest item is in its final place at the end; each later pass can ignore the items already placed. The list is sorted when a pass makes no swaps (or after n−1n-1 passes). Example: sort 8,3,6,2,58,3,6,2,5. Pass 1: 3,6,2,5,83,6,2,5,8. Pass 2: 3,2,5,6,83,2,5,6,8. Pass 3: 2,3,5,6,82,3,5,6,8. Pass 4: no swaps. The maximum number of comparisons for nn items is (n−1)+(n−2)+⋯+1=n(n−1)2(n-1)+(n-2)+\cdots+1=\frac{n(n-1)}{2}, so bubble sort is of order n2n^2.

Key termsbubble sort
Exam tip

Write out the whole list after each pass. A swap count and comparison count are easy to check from the passes.

Section 5

Quick sort

Quick sort chooses a pivot, the middle item: position N+12\frac{N+1}{2} if NN is odd and N+22\frac{N+2}{2} if NN is even. Items smaller than the pivot go to its left and larger ones to its right, each keeping the original order. The pivot is now in its final place. Repeat on each sublist with more than one item until every item is a pivot or alone. Example: 23,8,31,15,4,19,2723,8,31,15,4,19,27. N=7N=7, pivot 15: 8,4∣15∣23,31,19,278,4\mid15\mid23,31,19,27. Left, N=2N=2, pivot 4: 4,84,8. Right, N=4N=4, pivot 19: 19∣23,31,2719\mid23,31,27; then N=3N=3, pivot 31: 23,27∣3123,27\mid31; then pivot 27. Result: 4,8,15,19,23,27,314,8,15,19,23,27,31. Quick sort is usually much faster than bubble sort on long lists.

Key termsquick sortpivot
Common mistake

Reordering the sublists while splitting. Keep the original order within the left and right groups.

Section 6

Exam approach

Write every pass or trace step. Label pivots, state bin contents and totals, and give the number of bins. For order questions give the power of nn and use a ratio argument: multiply nn by kk and the time is multiplied by about k2k^2. Mention that estimates ignore lower-order terms.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on Algorithms, bin packing and sorting

  1. The following algorithm is applied to two positive integers aa and bb, where a>ba>b. Step 1: input aa and bb. Step 2: let rr be the remainder when aa is divided by bb. Step 3: if r=0r=0, output bb and stop. Step 4: replace aa by bb and replace bb by rr. Step 5: go back to Step 2.
    State what the algorithm finds, and explain why it must eventually stop.2 marks
  2. Nine files of sizes 7, 13, 8, 3, 5, 9, 4, 14 and 12 MB are to be saved on memory cards, each of capacity 20 MB. A file cannot be split between cards.
    Use the first-fit decreasing algorithm to pack the files. State the number of cards used and the contents of each.2 marks
  3. The list 23, 8, 31, 15, 4, 19, 27 is to be sorted into ascending order using quick sort. The pivot is always the middle item of the list or sublist: in position N+12\frac{N+1}{2} if NN is odd and N+22\frac{N+2}{2} if NN is even. Items smaller than the pivot go to its left and larger items to its right, each keeping their original order.
    Show the result of the first pass of quick sort, stating the pivot used.3 marks
See the full worksheet

Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).