What Is an Algorithm?
Properties of Algorithms
An algorithm is a finite sequence of well-defined, executable instructions for carrying out a task or solving a class of problems. You encounter algorithms when calculating a total, checking a password, or arranging records. An algorithm can be expressed in ordinary language, diagrams, or programming notation. Its defining properties concern what its instructions mean and accomplish, rather than how they are written.
What the specification tells you
Before judging a procedure, identify its specification. This tells you which starting situations are allowed and what counts as a successful result.
For example, an attendance procedure might accept a list of student identifiers and return the number of identifiers on that list. An empty list is a possible input if the specification permits it. Returning zero for that list is then a required result, rather than an optional special case.
An algorithm may receive no external input, but its starting information and required outcome must still be clear. Instructions that work only for one convenient example do not establish a method for every permitted input.
Properties to check
Clear instructions have definiteness. For example, “choose a suitable value” leaves a decision unresolved unless suitable has already been defined. Clear instructions may still include choices, provided the rules governing those choices are explicit.
Executable instructions have effectiveness. A step must be something the intended person or machine can actually carry out with the information and operations provided.
Finiteness means that the procedure stops after a finite number of steps for every permitted input. A finite written description does not guarantee a finite execution: a short instruction can repeat forever.
Check | Satisfactory case | Problematic case |
|---|---|---|
Clear instructions | Arrange names alphabetically by surname, then given name | Arrange names in a sensible order |
Executable steps | Read an existing entry from the supplied list | Read a nonexistent entry beyond the list |
Finiteness | Process each entry in a finite list once, then stop | Repeat an action without a reachable stopping condition |
Correct result | Return the number of entries actually present | Return a fixed guess regardless of the list |
A procedure is correct for its specification when it produces the required result for every permitted input. Stopping and producing the right result are separate checks. A procedure that stops quickly but returns the wrong count has not solved the stated problem.
How to check finiteness
For repeated instructions, look for a quantity that makes progress toward the stopping condition. A particularly useful case is a non-negative integer that decreases on every repetition.
Suppose a counter starts at a non-negative integer n. Before each repetition, the procedure checks whether the counter is positive. Each repetition reduces it by at least one without making it negative. If T(n) is the number of repetitions, then
There can be at most n decreases before the counter reaches zero. The stopping check then prevents another repetition. This gives a reason that covers every permitted starting value, including zero.
Finiteness does not mean fast. A procedure can require an impractically large but finite number of operations and still satisfy this property. Efficiency is a further question beyond whether the instructions stop and solve the specified task.
What to remember
- Identify the permitted inputs and required outputs first.
- Check that instructions are precise and individually executable.
- Explain why repetition stops for every permitted input.
- Check the result separately from the stopping behaviour.
Example: Checking a badge-packing algorithm
An event volunteer packs badges into packets holding at most five badges each. A valid input is any non-negative integer number of badges. The required output is the number of packets needed, using full packets whenever possible and one partial packet if necessary.
The proposed algorithm uses these instructions:
- Set the remaining badge count to the input and the packet count to zero.
- If no badges remain, report the packet count and stop.
- If more than five badges remain, pack five badges. Otherwise, pack all remaining badges.
- Subtract the number just packed from the remaining count and increase the packet count by one.
- Return to instruction 2.
The repeated section is a loop: a group of instructions that may execute more than once.
Example: Check the algorithm for an order containing 23 badges, then examine its stopping behaviour.
a) Trace the order containing 23 badges
Find the remaining badge count after each packet and state the reported packet count.
The first packet holds five badges, leaving 23−5= ________ badges. The second leaves 18−5= ________ badges. The third leaves 13−5= ________ badges, and the fourth leaves 8−5= ________ badges.
Because only three badges remain, the fifth packet holds those three badges. The remaining count becomes zero, so the next stopping check reports five packets.
The result is 5 packets: four full packets and one partial packet. Counting only the four full packets would leave three badges unpacked.
b) Check the input zero and explain finiteness
What does the algorithm report for zero badges? Explain why it stops for every valid input.
For an input of zero, initialization gives a remaining count of zero and a packet count of zero. Instruction 2 immediately reports 0 packets. The packing instructions are skipped.
For any positive remaining count, the next packet removes either five badges or all remaining badges. Therefore, each repetition reduces the remaining count by at least one and never makes it negative. Because the initial count is a finite non-negative integer, it cannot keep decreasing in this way forever.
If n is the initial number of badges and T(n) is the number of packets packed, then
Once no badges remain, the next check stops execution. This explains finiteness for every valid input, including zero.
c) Evaluate a proposed shortcut
A volunteer replaces instructions 3 and 4 with “Subtract five from the remaining count and increase the packet count by one.” The stopping check still requires the remaining count to equal zero. Does this altered procedure stop for 23 badges?
With the shortcut, the remaining counts after successive repetitions are
After four repetitions, three badges remain. Subtracting five then gives ________ rather than zero. Further subtractions move the count farther below zero.
To reach zero after an integer number k of repetitions, the shortcut would need
A repetition count must be a whole number, so this cannot occur. The altered procedure does not stop for an input of 23 badges. Its instructions are precise, but decreasing a counter does not guarantee that an exact stopping value will be reached.
A student reviews a proposed algorithm before implementing it. Match each property with the requirement that the student should check.
The produced result meets the specification for every permitted input.
Execution stops after a finite number of steps for every permitted input.
Each instruction has a precise meaning without an unspecified required decision.
Each individual action can be performed exactly in finite time with the available operations.
Definiteness
Finiteness
Correctness
Effectiveness
A scanner has 86 pages waiting. Before each repetition, it checks whether any pages remain. If they do, it scans 7 pages, or all remaining pages when fewer than 7 remain, and removes those pages from the waiting count. It stops when the waiting count is zero. How many repetitions occur? Give your answer in repetitions, as a whole number.