5 Basics of Computers · 5.1 Computer basics: number systems, codes, logic, algorithms and spreadsheets
5.1.1 Algorithms and flowcharts: symbols, properties and examples
An algorithm is a finite, step-by-step, unambiguous method for solving a problem; a flowchart draws the same steps with standard symbols joined by arrows.
- APPSC Paper I
- Advanced level
- 8 min read
- Free
Core facts
- Algorithm: a finite sequence of well-defined steps that turns input into the required output. The word comes from the 9th-century Persian mathematician al-Khwarizmi.
- Five properties (Knuth):
- Finiteness: it must stop after a finite number of steps.
- Definiteness: every step is precise and unambiguous.
- Input: zero or more inputs.
- Output: at least one output.
- Effectiveness: every step is basic enough to be done exactly, in finite time (with pen and paper).
- Ways to write an algorithm: plain-language steps, pseudocode (program-like English, no strict syntax) and flowchart (diagram).
- Flowchart symbols:
| Symbol | Shape | Use |
|---|---|---|
| Terminal | Oval / rounded rectangle | Start and Stop |
| Input/Output | Parallelogram | Read, Print |
| Process | Rectangle | Calculation, assignment |
| Decision | Diamond (rhombus) | Yes/No or True/False test |
| Flow line | Arrow | Direction of flow |
| Connector | Small circle | Joins parts on the same page |
| Off-page connector | Pentagon (home-plate), textbook convention | Continues on another page |
| Predefined process | Rectangle with double side lines | Call to a sub-routine |
- Decision box: one entry, two (or more) exits. All other boxes normally have one exit.
- Three control structures (Böhm–Jacopini, 1966): sequence, selection (if / if-else / case) and iteration (loops: while, do-while/repeat-until, for). Any algorithm can be built from these three.
- Flowchart conventions: flow from top to bottom, left to right; exactly one Start and at least one Stop; flow lines should not cross (use connectors).
Example 1. Which flowchart symbol is used for "Read A, B"? Solution. Input and output use the parallelogram. Answer: parallelogram
Example 2. An algorithm takes no input and prints "Hello" once. Which property does it still satisfy? Solution. Input may be zero; output must be at least one. Answer: it is a valid algorithm (zero inputs, one output)
Explained
1. Writing an algorithm in steps.
Example 3. Algorithm to find the larger of two numbers.
- Start.
- Read A, B.
- If A > B, then print A; otherwise print B.
- Stop.
Flowchart: oval Start → parallelogram Read A, B → diamond A > B? → Yes: parallelogram Print A; No: parallelogram Print B → both arrows meet → oval Stop.
2. Largest of three numbers (nested selection).
- Start. Read A, B, C.
- If A > B: if A > C, print A, else print C.
- Otherwise: if B > C, print B, else print C.
- Stop.
Any path through this flowchart uses exactly two decision boxes.
3. Loops: sum and factorial.
Example 4. Algorithm for N! (factorial).
- Start. Read N.
- F ← 1, I ← 1.
- While I ≤ N: F ← F × I; I ← I + 1.
- Print F. Stop.
Dry run for N = 5: F takes the values 1, 2, 6, 24, 120. Answer: 120
Flowchart: the test I ≤ N? is a diamond; the "Yes" branch goes to the process box F ← F × I, I ← I + 1 and an arrow loops back to the diamond; the "No" branch goes to Print F.
4. Counting loop iterations.
Example 5. How many times does the body of "for I = 1 to 10 step 3" run? Solution. I = 1, 4, 7, 10; next value 13 exceeds 10. Answer: 4 times
5. Pre-test and post-test loops.
| Loop | Test | Minimum runs |
|---|---|---|
| while / for | Before the body (entry-controlled) | 0 |
| do-while / repeat-until | After the body (exit-controlled) | 1 |
6. Swapping two values.
Example 6. A = 5, B = 3. Steps: A ← A + B; B ← A − B; A ← A − B. Final values? Solution. A = 8; B = 8 − 3 = 5; A = 8 − 5 = 3. Answer: A = 3, B = 5 (swapped without a third variable). The usual method uses a temporary variable: T ← A; A ← B; B ← T.
7. Dry run (trace table). Executing an algorithm by hand with sample values, writing each variable's value after each step, is called a dry run or desk check. It finds logical errors before coding.
Deeper
1. Euclid's algorithm for HCF (GCD).
- Read A, B.
- While B ≠ 0: R ← A mod B; A ← B; B ← R.
- Print A.
Example 7. Trace for A = 48, B = 18.
| Pass | A | B | R = A mod B |
|---|---|---|---|
| 1 | 48 | 18 | 12 |
| 2 | 18 | 12 | 6 |
| 3 | 12 | 6 | 0 |
After pass 3, A = 6 and B = 0. Answer: HCF 6, after 3 passes of the loop
2. Sum of digits.
Example 8. S ← 0; while N > 0: S ← S + (N mod 10); N ← N div 10. Output for N = 4729? Solution. Digits taken 9, 2, 7, 4. S = 9 + 2 + 7 + 4. Answer: 22
3. Searching and sorting (the usual degree-level examples).
| Method | Needs sorted data? | Worst-case comparisons |
|---|---|---|
| Linear search | No | n |
| Binary search | Yes | ⌊log₂ n⌋ + 1 (about log₂ n) |
| Bubble sort | — | n(n − 1)/2, in n − 1 passes |
Example 9. Maximum comparisons for a binary search in a sorted list of 1,000 items? (a) 9 (b) 10 (c) 100 (d) 500 Solution. Each comparison halves the list; worst case = ⌊log₂ 1000⌋ + 1 = 9 + 1 (since 2⁹ = 512 ≤ 1,000 < 1,024 = 2¹⁰). Answer: (b) 10
Example 10. Bubble sort on 6 numbers: how many passes and comparisons (basic version)? Solution. Passes = 6 − 1 = 5. Comparisons = 6 × 5 ÷ 2. Answer: 5 passes, 15 comparisons
4. Merits and limits of flowcharts.
- Merits: clear picture of logic, easy communication, help in debugging and documentation.
- Limits: tedious for large programs, hard to modify (redraw), no standard way to show detail; pseudocode is easier to change.
5. Algorithm efficiency. Two correct algorithms may differ in time (number of steps) and space (memory) used. Big-O notation gives the growth rate: linear search O(n), binary search O(log n), bubble sort O(n²).
Common traps
- ✗ "An algorithm must have at least one input." ✓ Zero or more inputs; at least one output.
- ✗ "Decisions use a rectangle." ✓ Diamond; the rectangle is for processing.
- ✗ "Input/output uses a rectangle with wavy base." ✓ Parallelogram; wavy base is a document.
- ✗ "Start/Stop is a circle." ✓ Oval (terminal); a small circle is a connector.
- ✗ "Binary search works on any list." ✓ The list must be sorted.
- ✗ "A while loop always runs at least once." ✓ It may run zero times; do-while/repeat-until runs at least once.
- ✗ "Finiteness means the input is finite." ✓ It means the algorithm terminates after finitely many steps.
Quick revision
- Algorithm: finite, definite, effective; 0+ inputs, 1+ outputs.
- Oval = start/stop; parallelogram = input/output; rectangle = process.
- Diamond = decision (two exits); circle = on-page connector; pentagon = off-page connector.
- Flow top to bottom, left to right; arrows show direction.
- Three structures: sequence, selection, iteration.
- while: test first (0+ runs); do-while: test last (1+ runs).
- Dry run = tracing by hand with a trace table.
- Binary search needs sorted data; about log₂ n comparisons.
- Bubble sort: n − 1 passes, n(n − 1)/2 comparisons.
- Euclid: HCF(48, 18) = 6.
Go deeper: Mains writing (beyond what APPSC Paper I usually asks)
Mains writing Mains writing
- Frame: an algorithm is the logic; a program is that logic written in a language a computer can run.
- Steps in problem solving: understand the problem → identify input and output → design the algorithm → draw the flowchart or write pseudocode → dry run → code → test → document.
- Statistics office link: routine tasks such as tabulating survey schedules, computing averages and checking data for errors are first designed as algorithms, then put into spreadsheets or programs.
Practise this topic. Questions on Arithmetic & logic operations, algorithms & flowcharts, with answers explained.
All Computer basics: number systems, codes, logic, algorithms and spreadsheets notes · APPSC Paper I syllabus · APPSC Paper I Mock 1 with answers · Start free
Medha AI writes its own notes and checks them against the sources listed. Medha AI is an independent preparation service. It is not affiliated with, endorsed by or connected to APPSC, TSPSC, MPSC, UPSC or any other commission or government body.