D1 1.1 - Algorithms and their order

D1 1.1 - Algorithms and their order

An algorithm is a precise set of instructions that turns inputs into outputs. You will follow instructions given in text or a flowchart, record changing values in a trace table, and use an algorithm's order to estimate how its running time grows.

Instructions, assignments and decisions

Must adding the first million positive integers require a million additions? One algorithm adds the integers one at a time. Another evaluates n(n+1)/2n(n+1)/2. They produce the same answer, but the amount of work grows very differently.

Algorithm

An algorithm is a finite, unambiguous sequence of instructions for carrying out a task. It specifies what to do, which decision to take, and when to stop. “Keep improving the answer” is not a complete algorithm unless “improving” and the stopping condition are defined.

An input supplies starting information; an output is a reported result. A variable stores a value that may change during the process. An instruction x←x+1x\leftarrow x+1 means “replace the stored value of xx by its current value plus one”. It is an assignment, not an equation to solve.

Execute instructions in order. With x=1x=1, y=2y=2, the instructions x←x+yx\leftarrow x+y, then y←x−yy\leftarrow x-y, give x=3x=3, then y=1y=1. The second instruction uses the updated xx, not its old value.

A flowchart expresses the same ideas graphically: an oval marks start or stop, a parallelogram input or output, a rectangle a process, and a diamond a decision with labelled branches. Arrows specify the order, including any return to an earlier step.

Key idea

Use the current stored values and follow the stated branch. Do not silently replace the given procedure by a different one that reaches the same final answer.

The order of assignments is the whole point of this short diagnostic.

Tracing a text algorithm

A trace table records the values actually produced as instructions are executed. Keep the entries in reading order so someone else can reconstruct the same process.

Consider this algorithm for a positive integer input nn:

  1. Set s=0s=0, j=1j=1.
  2. If j>nj>n, go to Step 5.
  3. Set s←s+js\leftarrow s+j.
  4. Set j←j+1j\leftarrow j+1, then return to Step 2.
  5. Output ss and stop.

The test occurs before the addition. The row with j=n+1j=n+1 is reached, but its value is not added.

Worked example 1: three additions

Question. Apply the algorithm with n=3n=3, showing its trace and output.

Plan. Record the initial state and the state after each completed update, then test whether another iteration is allowed.

StagejjssResult of j>3j>3
Initial state1100No
After adding 11 and increasing jj2211No
After adding 22 and increasing jj3333No
After adding 33 and increasing jj4466Yes: stop

Why include the last row? The algorithm only knows it is finished after the next test succeeds.

Answer. The output is s=6s=6.

Check. The additions were 1+2+3=61+2+3=6; the value 44 was tested but never added.

Worked example 2 changes only the input, from 33 to 44. One further pass through the loop is now allowed.

Worked example 2: a larger stopping value

Question. Apply the same algorithm with n=4n=4.

Plan. Follow the same updates, continuing through the addition of 44.

Statejjss
Initial1100
After first loop2211
After second loop3333
After third loop4466
After fourth loop551010

At j=5j=5, the condition j>4j>4 is true, so the algorithm outputs rather than adding again.

Why not stop at j=4j=4? The condition is j>nj>n, not j≥nj\ge n; equality still permits an addition.

Answer. s=10s=10.

Check. 1+2+3+4=101+2+3+4=10.

If a question supplies a partly completed table, follow its headings and layout. A row after every update is acceptable, but updates must appear in execution order. Avoid rounding intermediate values unless the instructions require it.

Watch out

Wrong: change the stopping condition from j>nj>n to j≥nj\ge n. Right: test the inequality exactly as written. One changed symbol can omit the final required iteration.

Use the same instructions with a new input.

Tracing decisions in a flowchart

At a decision diamond, take one branch and follow its arrow. A return arrow can cause the same instruction to be executed many times.

The flowchart below repeatedly halves an integer, discarding any fractional part, until it is below 22. The notation ⌊u⌋\lfloor u\rfloor means the greatest integer not exceeding uu; for example, ⌊21/2⌋=10\lfloor21/2\rfloor=10.

[DIAGRAM: asset_name: A loop that counts repeated integer halvings; asset_slug: 9fm0-d1-161-halving-flowchart; caption: Test before halving, update the counter, then return to the decision; description: Start flows to input positive integer N, then c=0, then a decision N at least 2. The Yes branch goes to N=floor(N/2), then c=c+1, and loops to the decision. The No branch outputs c and stops. Standard flowchart shapes and clear Yes and No labels are used.]
Test before halving, update the counter, then return to the decision

Worked example 3: an even starting value

Question. Trace the flowchart for N=20N=20.

Plan. Record N,cN,c after each halving, returning to the decision each time.

StageNNccDecision
Initialise202000Yes
First halving101011Yes
Second halving5522Yes
Third halving2233Yes
Fourth halving1144No

Why does 55 become 22? The process uses the integer part, not rounding to the nearest integer.

Answer. The output is 44.

Check. Four halvings were performed. The final value 11 fails N≥2N\ge2.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account