
Let’s think it through · Start with a story
The girl sorts the same five number cards by hand, while the robot sorts them with a program. Can they use the same method?
Break down the process
- ProblemOrder the cards from low to high
- AlgorithmRepeatedly pick the smallest
- ProgramWrite those steps in C++
An algorithm is a clear set of steps for solving a problem; a program expresses those steps in a form a computer can execute.
The same sorting algorithm can be demonstrated on paper or implemented in different programming languages. It is like writing the same recipe in different languages.
01What is an algorithm?
An algorithm is a clear set of steps for solving a problem. It is about “how to do it” and does not have to be written in any particular programming language.
For example, to find the highest score, start by treating the first score as the current highest. Compare each score in turn and update the highest whenever you find a larger one.
02What is a program?
A program is an implementation written in a programming language that a computer can execute. A real program usually also handles input and output, organizes data, handles errors, and more.
03What is the biggest difference?
Algorithm
A method and a set of steps for solving a problem.
Program
An executable implementation of the algorithm and any other required logic.
04Can one algorithm be written as many programs?
Yes. The same sorting algorithm can be implemented in C++, Python, Java, and other languages. Even within one language, it can be written in different ways.
05Can one program contain many algorithms?
Of course. A game might use several algorithms at once, including pathfinding, collision detection, sorting, and searching.
06Why do algorithms matter?
Different algorithms can produce the same correct answer but differ greatly in speed and memory use.
Correctness
Produce the right result.
Efficiency
Finish the task in less time.
Resource use
Use memory and other resources wisely.
What you have learned
- An algorithm is a clear method and set of steps for solving a problem.
- A program is an executable implementation and usually includes more than the algorithm.
- One algorithm can have many program implementations.
- One program can contain many algorithms.
- Algorithms need to be correct, and often need to be efficient as well.
Your turn
If you rewrite a C++ program in another language but keep the same steps for choosing the smallest number, has the algorithm changed?
Have an answer? Click to see the explanation
No. The language used to express the steps changed, but the method did not. If you instead sort by repeatedly swapping neighboring cards, that is a different sorting method.