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 ; = remainder of ; if output ; otherwise replace by , by and repeat. For , the remainders are 42, 21, 0, giving output 21, the highest common factor.
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 . The order of an algorithm describes how the running time grows with for large : it keeps only the highest power. An algorithm of order takes about 100 times as long if is multiplied by 10. Bubble sort and first-fit bin packing are of order ; a quadratic count such as has order . Common orders from fastest to slowest growth: , , , , . The order is a guide to efficiency, not an exact time: lower-order terms and constants are ignored.
Saying the time doubles when doubles for an order 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 total 75, so at least 4 bins. First-fit uses 5 bins: , , , , ; first-fit decreasing uses 4: , , , . These are heuristics: none guarantees the optimum, except when the bound is met.
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 passes). Example: sort . Pass 1: . Pass 2: . Pass 3: . Pass 4: no swaps. The maximum number of comparisons for items is , so bubble sort is of order .
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 if is odd and if 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: . , pivot 15: . Left, , pivot 4: . Right, , pivot 19: ; then , pivot 31: ; then pivot 27. Result: . Quick sort is usually much faster than bubble sort on long lists.
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 and use a ratio argument: multiply by and the time is multiplied by about . 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
- The following algorithm is applied to two positive integers and , where . Step 1: input and . Step 2: let be the remainder when is divided by . Step 3: if , output and stop. Step 4: replace by and replace by . Step 5: go back to Step 2.State what the algorithm finds, and explain why it must eventually stop.2 marks
- 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
- 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 if is odd and if 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
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).