After this chapter you will be able to
- define an algorithm and list its properties
- draw and read flowcharts using standard symbols
- use the three control structures
- explain basic data types, variables and operators
- describe OOP concepts and common data structures
- outline the software development life cycle and testing types
16.1 Algorithm
An algorithm is a finite, step-by-step procedure to solve a problem.
- The word comes from the Persian mathematician al-Khwarizmi (9th century).
- An algorithm does not depend on any language. The same logic can be written in pseudocode, a flowchart, or any programming language.
Properties of a good algorithm
| Property | Meaning |
|---|---|
| Input | Zero or more inputs |
| Output | At least one output |
| Definiteness | Each step is clear and unambiguous |
| Finiteness | Ends after a limited number of steps |
| Effectiveness | Each step is simple enough to be carried out |
| Language independence | The same algorithm can be written in any programming language |
Example — find the larger of two numbers
- Start.
- Read A and B.
- If A > B, print A; otherwise print B.
- Stop.
Algorithm vs program: an algorithm is the plan for solving a problem; a program is that plan written in a programming language, so that a computer can run it. A list of coded instructions is a program.
- Steps are executed in order, from the first step to the last (top to bottom).
- Complexity measures how much time and memory an algorithm needs as the input grows. It describes efficiency (performance), not whether the answer is correct.
Types of Algorithms
| Type | Idea |
|---|---|
| Recursive | Solves a problem by calling itself with smaller inputs |
| Divide and conquer | Splits the problem into smaller sub-problems, solves each one, then combines the answers |
| Greedy | Always takes the best local (immediate) choice at each step |
| Backtracking | Tries a path and goes back to try another when a path fails |
| Dynamic programming | Solves overlapping sub-problems once and stores the results for reuse |
| Brute force | Checks every possible option and picks the best |
| Randomized | Uses random numbers to decide the next step |
| Hashing | Converts data into a fixed-size value (hash) for fast lookup |
16.2 Flowchart
A flowchart is a diagram of an algorithm using standard symbols.
- OvalTerminal — Start / Stop
- ParallelogramInput / Output
- RectangleProcess
- DiamondDecision (Yes / No)
- ArrowFlow line
- Small circleOn-page connector
- PentagonOff-page connector
- Double-sided rectanglePredefined process (sub-routine)
- Wavy-bottom rectangleDocument
Sample: Start → Read A, B → Is A > B? → Yes: Print A / No: Print B → Stop.
| Symbol | Name | Use |
|---|---|---|
| Oval (rounded) | Terminal | Start / Stop |
| Parallelogram | Input / Output | Read, print |
| Rectangle | Process | Calculation, assignment |
| Diamond | Decision | Yes/No or True/False question |
| Arrow | Flow line | Direction of flow |
| Small circle | Connector | Join parts on the same page |
| Rectangle with double sides | Predefined process | Call a sub-program |
| Bracket-shaped box joined by a dashed line | Comment | Explanatory note; it does not change the flow |
| Cylinder | Database | Data stored in a database or file |
★ EXAM POINT — Diamond = decision; parallelogram = input/output; oval = start/stop; rectangle = processing.
Other Planning Tools
| Tool | Meaning |
|---|---|
| Pseudocode | Algorithm written in simple English-like statements without strict syntax |
| Decision table | Table showing conditions and the actions for each combination |
| Data Flow Diagram (DFD) | Shows how data moves through a system |
| UML (Unified Modeling Language) | Standard visual notation for designing software: classes, objects and their relationships |
Limitation: a long or complex flowchart becomes clumsy and hard to follow. This is why pseudocode is often used for big logic.
Types of Flowcharts
- Process flowchart: the sequential steps of a process and the decisions needed to make it work.
- System flowchart: shows how data flows through a system and how decisions control its events.
- Decision flowchart: helps choose between alternative courses of action.
- Product flowchart: the sequence of events needed to manufacture a product.