D1.1 - Algorithms
- Syllabus
- 2019
- Topic
- D1.1
- Level
- AS
An algorithm is a precise, finite sequence of instructions that transforms permitted inputs into outputs. To implement an algorithm written as text or a flow chart, follow its arrows and decisions exactly, updating variables in the stated order until its stopping condition is reached.
Make a trace table with one row for each completed pass through the process. Record every variable after the assignments, the result of the decision test, and any output. Use the updated value in the next row; do not reuse the previous value or stop merely because successive values look close.
For the text algorithm 'input x; while x<20, replace x by x+3; output x', input 8 gives successive values 8, 11, 14, 17, 20. The test is then false, so the output is 20.
\text{middle position}=\begin{cases}(N+1)/2,&N\text{ odd},\(N+2)/2,&N\text{ even}.\end{cases}
Positions are counted from 1. Thus a list of 9 items uses the 5th item, while a list of 6 uses the 4th: the right-hand one of the two central items. Apply the same rule whenever a middle item is required.
Check that the input satisfies every stated condition and that the algorithm actually reaches its exit. Analysis of the order or computational complexity of an algorithm is not required in this unit.
These four list algorithms have different jobs: bin packing allocates, bubble and quick sort order, and binary search locates. Preserve the given order unless the method explicitly changes it.
| Algorithm | Required method | Completion or validity check |
|---|---|---|
| first-fit bin packing | in given order, place each item in the first bin with room | capacity is never exceeded |
| first-fit decreasing | sort descending, then apply first-fit | reaching ⌈total/capacity⌉ bins proves optimality |
| bubble sort | scan adjacent pairs from the left; swap wrong-order pairs; repeat | show a pass with no swaps |
| quick sort | choose the middle pivot, partition, then repeat on each sublist | mark pivots; stop at sublists of size 0 or 1 |
| binary search | on a sorted list, compare the target with the middle and reject the impossible half | target found or no candidates remain |
For every quick-sort sublist and binary-search candidate list, use (N+1)/2 when N is odd and (N+2)/2 when N is even, so an even list uses its right-hand middle item.
Quick-sorting [7,2,5,3,6] starts with pivot 5: [2,3]∣5∣[7,6]. To binary-search sorted [2,3,5,6,7] for 6, compare with 5 and search [6,7]; its right-middle item is 7, then 6 is found.
Binary search cannot use an unsorted list, and first-fit decreasing sorts before packing. Reaching the bin lower bound proves optimality; using more bins does not by itself prove that fewer are possible.