Published by Patrick Mutisya · 14 days ago
Show understanding that an algorithm is a solution to a problem expressed as a sequence of defined steps.
An algorithm is a finite, well‑defined set of instructions that, when followed, solves a particular problem or performs a specific task. It must be:
| Characteristic | Description |
|---|---|
| Correctness | The algorithm produces the required output for all valid inputs. |
| Efficiency | Measured in terms of time (how fast) and space (how much memory) required. |
| Generality | Works for a broad class of inputs, not just a single case. |
| Determinism | Given the same input, it always follows the same sequence of steps. |
Algorithms can be expressed in several forms, each useful for different purposes.
Pseudocode is a high‑level, language‑independent description that mixes natural language with programming constructs.
// Example: Find the maximum value in an array A[1..n]
INPUT: array A, integer n
max ← A[1]
FOR i ← 2 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
OUTPUT max
Flowcharts use standardized symbols to illustrate the control flow of an algorithm.
Some algorithms are described using formal mathematical expressions. For example, the Euclidean algorithm for the greatest common divisor (GCD) can be written as:
\$\$\text{gcd}(a,b) =
\begin{cases}
a, & \text{if } b = 0\\[4pt]
\text{gcd}(b, a \bmod b), & \text{otherwise}
\end{cases}\$\$
Searches for a target value \$x\$ in an unsorted list \$L\$ of length \$n\$.
Assumes \$L\$ is sorted in ascending order.
INPUT: sorted array L[1..n], target x
low ← 1
high ← n
WHILE low ≤ high DO
mid ← ⌊(low + high) / 2⌋
IF L[mid] = x THEN
OUTPUT mid
STOP
ELSE IF L[mid] < x THEN
low ← mid + 1
ELSE
high ← mid - 1
END IF
END WHILE
OUTPUT “not found”
Two primary measures are used:
Big‑O notation provides an upper bound on growth rates. For example:
Algorithms are the bridge between problem statements and executable programs. Understanding how to design, represent, and analyse algorithms equips students to: