Algorithms & Flowcharts
Designing a solution before writing any code.
What makes an algorithm
An algorithm is a finite sequence of precise steps that solves a problem.
Four requirements:
- Precise, each step is unambiguous.
- Finite: it terminates.
- Effective: each step can actually be carried out.
- General: it works for any valid input, not one example.
That last point is what separates an algorithm from a solution to one particular case.
Flowchart symbols
- Oval: start or stop.
- Parallelogram: input or output.
- Rectangle: a process or calculation.
- Diamond: a decision, with exactly two exits labelled yes and no.
- Arrows: the direction of flow.
Why diagram it first: a mistake in a flowchart takes seconds to fix. The same mistake found in code can take an hour, and may not announce itself at all.
Every flowchart has exactly one start and at least one stop.
Pseudocode
Pseudocode writes an algorithm in structured English, precise enough to be unambiguous, but not tied to any language.
BEGININPUT n
SET total = 0
FOR i = 1 TO n
total = total + i
END FOR
OUTPUT total
END
Its advantage over a flowchart: it handles loops and nesting far more compactly, and translates almost directly into any programming language.
Flowcharts are better for showing branching visually; pseudocode is better for anything long.
Designing an algorithm from scratch
Problem. Find the largest number in a list.
The trap: you glance at a list and see the largest instantly. A computer sees one value at a time, so the algorithm must reflect that constraint.
The design:
SET largest = first itemFOR each remaining item
IF item > largest THEN
SET largest = item
END IF
END FOR
OUTPUT largest
Why start with the first item rather than zero? If every number were negative, starting at zero would return zero, which is not in the list. Starting with a real value avoids that.
Now trace it by hand on [4, 9, 2, 7]. Then test the edge cases: one item, all equal, all negative. This is where algorithms break.
Searching and sorting
Linear search: check every item until you find it. Works on any list; slow for long ones.
Binary search: only on a sorted list. Check the middle; if the target is smaller, discard the entire upper half, and repeat.
The difference in scale is dramatic: in a list of 1,000,000 items, linear search may need a million checks. Binary search needs about 20.
Each step halves the remaining possibilities, which is why it is so fast, and why the list must be sorted first.
Bubble sort repeatedly compares neighbouring pairs and swaps them if out of order. Simple to understand and inefficient for large lists.
The lesson: the choice of algorithm matters more than the speed of the computer.
Testing an algorithm properly
An algorithm that works on your example may still be wrong.
Three kinds of test data:
- Normal, typical values. A mark of 65 for a pass/fail program.
- Boundary: exactly on the limit. 49, 50 and 51 if the pass mark is 50. Most errors live here, because of confusion between > and ≥.
- Erroneous: invalid input. A negative mark, a mark of 150, or letters instead of numbers.
Trace tables are the tool for checking by hand: a column for each variable, a row for each pass through the loop. Fill it in manually and you will see exactly where the values go wrong.
The habit worth building: when you think an algorithm is finished, deliberately try to break it. Testing only the case you designed for proves nothing.
Where algorithmic thinking helps
Beyond programming. Breaking a large problem into precise steps, decomposition, works on essays, experiments and revision plans.
Recognising patterns. Noticing that a new problem resembles one you have solved is how experienced programmers work quickly.
Everyday algorithms. Recipes, assembly instructions and directions are all algorithms, and the good ones share the same properties: precise, finite, ordered.
Understanding the systems around you. Search results, recommendations and content feeds are all algorithms making decisions. Knowing they are designed by people, with particular goals, is worth more than knowing any specific one.
Where it usually goes wrong
- "An algorithm is code." It is the plan; code is one expression of it.
- Writing code before designing.
- Binary search on an unsorted list. It gives wrong answers silently.
- Testing only normal data.
- Decision boxes with three exits. Exactly two.
- Loops with no way to terminate.
What to remember
- Algorithms are precise, finite, effective, general.
- Design first: flowchart for branching, pseudocode for length.
- Binary search needs a sorted list and is dramatically faster.
- Test normal, boundary and erroneous data.
Watch one worked through
Here is one done slowly, with the reason for every move. Then one for you.
Draw the logic of a flowchart that reads a number and prints whether it is even or odd.
- Start, then input the number.(Start) โ [ INPUT n ]Every flowchart has exactly one start. An oval is for start and stop, a parallelogram for input and output.
- Add the decision as a diamond with a yes and a no branch.< n รท 2 has remainder 0 ? >A decision box must have exactly two exits. If a question cannot be answered yes or no, it is not a decision box.
- Send each branch to its own output.YES โ [ PRINT "Even" ] NO โ [ PRINT "Odd" ]Both branches must lead somewhere. A path that just stops is the commonest mistake in a drawn flowchart.
- Rejoin the branches and stop.both โ (Stop)Bringing the paths back together keeps one exit, which matters as soon as the chart grows.
Answer: Start, input n, decision on whether the remainder after dividing by 2 is zero, print Even on yes and Odd on no, then stop
Which flowchart shape is used for a decision? One word.
Show the answer
diamond
Practise Algorithms & Flowcharts
Everything on QuizByt for Class 8 Computer Science that goes with this lesson: read it first, then test yourself while it is fresh.
๐ง 17 quizzes on Algorithms & Flowcharts (14 easy, 2 medium, 1 hard)
Work up from easy to hard: the hard quiz asks the Class 8 exam-style questions on Algorithms & Flowcharts.
๐จ๏ธ Printable worksheet
Algorithms & Flowcharts on paper for Class 8, with room to write the working out. Preview the first page free.
๐ฎ Games for Class 8 Computer Science
Memory and reasoning games at the right level for Class 8. Passing a quiz on Algorithms & Flowcharts earns the credits that open them.
Now test yourself
You have read the whole lesson. Take the quiz on this topic to see how much stuck,a free account is all it takes.
- ๐ Your score is saved, so you can see progress over time
- ๐ See exactly which questions you got wrong, and why
- ๐ Earn credits for every quiz you pass, and spend them on games
In a hurry? Try a free Computer Science quiz with no account.