Computer Science · Ch 4 — Introduction to Problem Solving
Algorithm
Algorithm
Everyday life is full of activities we complete by following a fixed sequence of steps — getting ready for school, making breakfast, riding a bicycle, wearing a tie, solving a puzzle. Each of these has its own ordered list of actions, and doing the actions in order accomplishes the activity.
Everyday example: riding a bicycle
The activity "riding a bicycle" can be broken down into a sequence like this:
- Remove the bicycle from the stand.
- Sit on the seat of the bicycle.
- Start pedalling.
- Use brakes whenever needed.
- Stop on reaching the destination.
Nothing about this is mathematical — it is simply an ordered, finite list of steps that gets a task done.
Worked example: GCD of 45 and 54
The same step-by-step thinking applies to a computational task. The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them. To find the GCD of 45 and 54:
Step 1 — list the divisors of each number (the numbers that divide it exactly):
Divisors of 45: 1, 3, 5, 9, 15, 45
Divisors of 54: 1, 2, 3, 6, 9, 18, 27, 54
Step 2 — pick the largest number common to both lists.
The common divisors are 1, 3 and 9; the largest is 9.
GCD(45, 54) = 9
What makes such a sequence an algorithm
Both examples show that accomplishing a task means following a sequence of steps. Such a finite sequence of steps required to get the desired output is called an algorithm. Its defining properties: …