Topics in this sectionAlgorithms2 topics
- Algorithm Fundamentals
- Translating Algorithms to Programs
Algorithms: Step-by-Step Instructions for Computers
An algorithm is a clear, step-by-step set of instructions that solves a specific problem or completes a task. Think of it like a recipe in a cooking book — you follow each step in order, and at the end you get a finished dish. In the same way, a computer follows an algorithm to transform input data into useful output.
Every algorithm has three main parts:
- Input — the data you give to the computer to work with
- Processing — the calculations or decisions the computer performs
- Output — the final result the computer shows you
For example, when you calculate the total cost of items at a boma market, the prices you enter are the input, the addition is the processing, and the total price is the output.
A well-written algorithm has several important qualities:
Clarity — Each step must be clear and unambiguous. Anyone reading the algorithm should understand exactly what to do.
Finiteness — The algorithm must eventually end. It should not go on forever without producing a result.
Correctness — The algorithm must produce the right answer for any valid input.
Efficiency — The algorithm should not waste time or resources. It should solve the problem using a reasonable number of steps.
Completeness — The algorithm must consider all possible situations, including special cases like zero or negative numbers.
All algorithms are built from three fundamental building blocks:
Sequence — Steps are carried out one after another, in the order they are written. This is like following a shopping list from top to bottom.
Selection — The algorithm makes a decision based on a condition. It chooses one path or another depending on whether something is true or false. For example, "If the number is greater than 10, do this; otherwise, do that."
Iteration — A set of steps is repeated one or more times. This is useful when you need to process many items, like adding up all the numbers in a list.
These three structures can be combined to solve any problem.
When writing an algorithm, follow these steps:
-
Understand the problem — Know exactly what the algorithm should accomplish.
-
Identify the inputs — Determine what data you need to start with.
-
Determine the outputs — Know what result you want to produce.
-
Plan the processing steps — Break the solution into clear, logical steps using sequence, selection, and iteration.
-
Write each step clearly — Use simple language that describes exactly what happens at each stage.
-
Test with examples — Try your algorithm with different inputs to make sure it works correctly.
Here is an algorithm to find the largest of three numbers:
Step 1: Input three numbers. Call them A, B, and C.
Step 2: Compare A and B. If A is greater than B, set a variable called Largest to A. Otherwise, set Largest to B.
Step 3: Compare Largest with C. If C is greater than Largest, set Largest to C.
Step 4: Output the value of Largest.
This algorithm uses selection (the "if" comparisons) to make decisions and sequence to order the steps.
To find the average of five numbers:
Step 1: Input the five numbers.
Step 2: Add all five numbers together. Call this sum S.
Step 3: Divide S by 5 to get the average.
Step 4: Output the average.
This algorithm uses sequence — each step follows the previous one in order.
To determine whether a number is even or odd:
Step 1: Input a number. Call it N.
Step 2: Divide N by 2.
Step 3: Check the remainder. If the remainder is 0, the number is even. Otherwise, the number is odd.
Step 4: Output the result (Even or Odd).
This algorithm uses selection to check the remainder and decide which message to output.
Algorithms are often written in pseudocode — a simple way of describing steps that looks like a mixture of English and programming. Pseudocode is not actual code that runs on a computer, but it helps you plan how the program will work.
Here is the even/odd algorithm in pseudocode:
INPUT N
IF N MOD 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
END IF
The words INPUT, IF, THEN, ELSE, and OUTPUT help clearly show what happens at each step.
Try writing an algorithm for each of the following problems:
- Given three numbers, find the smallest one.
- Given five test scores, count how many are above 80.
- Given a temperature in Celsius, output whether it is hot (above 30), warm (between 15 and 30), or cold (below 15).
Remember to identify the input, processing, and output for each problem, and use sequence, selection, and iteration as needed.
In South Sudan, algorithms are used in everyday situations without people realizing it. When a shop owner at the Juba Main Market calculates the total price of several bags of sorghum using a simple calculator, the calculator follows an algorithm — it takes the prices as input, adds them through processing, and displays the total as output. Mobile money services in South Sudan also use algorithms to verify transactions and update account balances, helping people send and receive money securely across different counties.
Question
What are the three main parts of every algorithm?
Sign in to submit your answer and have it count toward your mastery.
Sign in to practiceTutor
Stuck? Ask me anything about this topic.
Sign in to ask the Sonza AI Tutor about this question.
Sign in to askDiscussions
No questions yet