An algorithm is a list of clear steps that solves a problem. You follow algorithms every day without calling them that. Making tea, booking a train ticket and finding a word in a dictionary all follow a fixed set of steps.
This page explains what an algorithm is with everyday examples. Then it shows how to write the steps, draw a simple flowchart, and turn the steps into a small program in Java and Python.
An everyday algorithm: making tea
Here are the steps to make one cup of tea:
- Start.
- Pour one cup of water into a pan.
- Heat the water until it boils.
- Add tea powder and sugar.
- Boil for two minutes.
- Add milk and boil for one more minute.
- Strain the tea into a cup.
- Stop.
This is an algorithm. It has a clear start and end. Each step is simple. The order matters: you cannot strain the tea before you boil it. And anyone who follows the steps gets the same result.
Computers need the same thing, but with even more precise steps. A computer cannot guess what "a little sugar" means. It needs "one teaspoon".
Algorithm meaning in programming
In programming, an algorithm is a finite set of well-defined steps that takes some input, does some work, and produces an output.
| Problem | Input | Output |
|---|---|---|
| Add two numbers | Two numbers | Their sum |
| Find the largest of three numbers | Three numbers | The biggest one |
| Check if a number is even | One number | Yes or No |
| Search a name in a list | A list and a name | Position of the name, or "not found" |
| Sort marks from high to low | A list of marks | The same marks in order |
The word "algorithm" comes from the name of al-Khwarizmi, a ninth-century Persian mathematician whose books explained step-by-step methods for calculation.
Properties of a good algorithm
A set of steps is a proper algorithm only if it has these properties:
| Property | Meaning | Tea example |
|---|---|---|
| Input | Takes zero or more inputs | Water, tea powder, milk, sugar |
| Output | Produces at least one result | A cup of tea |
| Definiteness | Each step is clear, with one meaning | "Boil for two minutes", not "boil for some time" |
| Finiteness | Ends after a limited number of steps | Step 8 is Stop |
| Effectiveness | Each step can actually be done | Every step is possible in a kitchen |
Interview note: "What are the characteristics of an algorithm?" is a common viva and interview question. Remember the five words above: input, output, definiteness, finiteness, effectiveness.
How to write an algorithm: largest of three numbers
Let us solve a real programming problem. Given three numbers, find the largest.
Step 1: Understand the problem. Input is three numbers, say A, B and C. Output is the biggest one.
Step 2: Think of how you would do it by hand. Look at A and B, keep the bigger one. Compare that with C. Keep the bigger one. That is the answer.
Step 3: Write the steps clearly.
Algorithm: Largest of three numbers
1. Start
2. Read A, B, C
3. Set LARGEST = A
4. If B > LARGEST, set LARGEST = B
5. If C > LARGEST, set LARGEST = C
6. Print LARGEST
7. Stop
Step 4: Test the steps by hand. Take A = 12, B = 45, C = 30.
| Step | Check | LARGEST |
|---|---|---|
| 3 | Start with A | 12 |
| 4 | Is 45 > 12? Yes | 45 |
| 5 | Is 30 > 45? No | 45 |
| 6 | 45 |
The answer is 45, which is correct. Also test edge cases, such as all three numbers equal, or the largest one coming first. Testing before coding saves a lot of debugging time.
Algorithm and flowchart
A flowchart draws the algorithm as a picture. Each shape has a fixed meaning.
| Shape | Meaning |
|---|---|
| Oval | Start or Stop |
| Parallelogram | Input or output (Read, Print) |
| Rectangle | A process step (calculation, assignment) |
| Diamond | A decision with Yes and No paths |
| Arrow | The direction of flow |
Here is the flowchart for the largest of three numbers, drawn in text:
( Start )
|
/ Read A, B, C /
|
[ LARGEST = A ]
|
< B > LARGEST ? > --Yes--> [ LARGEST = B ]
| No |
|<------------------------+
|
< C > LARGEST ? > --Yes--> [ LARGEST = C ]
| No |
|<------------------------+
|
/ Print LARGEST /
|
( Stop )
In this drawing, ( ) is an oval, / / is a parallelogram, [ ] is a rectangle and < > is a diamond. On paper, draw the real shapes. Flowcharts are especially useful for showing decisions and loops to someone who does not read code.
From algorithm to code
An algorithm does not depend on any language. Once the steps are right, writing code is mostly translation. Here are the same seven steps in Java and Python.
Java
public class LargestOfThree {
public static void main(String[] args) {
int a = 12, b = 45, c = 30;
int largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
System.out.println("Largest: " + largest);
}
}
Python
a, b, c = 12, 45, 30
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print("Largest:", largest)
Output (same for both):
Largest: 45
Line by line, the code matches the written steps. That is the goal. If the algorithm is correct, the code is easy. If you start coding without a plan, you often get lost. The full problem, with more approaches and edge cases, is on the largest of three numbers page.
A second example: linear search
Linear search finds a value in a list by checking each item from the start. It uses a loop.
Algorithm: Linear search
1. Start
2. Read the list and the TARGET value
3. For each position i from 0 to the last position:
If list[i] equals TARGET, print i and stop
4. Print "Not found"
5. Stop
names = ["Ravi", "Sita", "Arun", "Neha"]
target = "Arun"
position = -1
for i in range(len(names)):
if names[i] == target:
position = i
break
if position == -1:
print("Not found")
else:
print("Found at index", position)
Output:
Found at index 2
If the list has 1,000 names and the target is last, linear search checks all 1,000. Binary search is much faster on a sorted list, because it throws away half of the remaining items at every step. Practise both: linear search and binary search.
Why some algorithms are better than others
Many algorithms can solve the same problem. They differ in how much time and memory they use.
| Problem | Slower algorithm | Faster algorithm |
|---|---|---|
| Find a word in a sorted dictionary | Read every page from the start | Open the middle, then go left or right (binary search) |
| Sort 10 lakh numbers | Bubble sort | Merge sort or quick sort |
On 10 items you will not see a difference. On lakhs of items, the faster algorithm can finish in seconds while the slower one takes far longer. Measuring this is called time complexity, and it is a key topic in interviews.
Algorithms and data structures
An algorithm works on data, and how that data is stored changes which algorithm is best. Binary search needs a sorted array. A stack makes the undo feature simple. This is why the two are learned together as DSA. Read what are data structures for the other half, and the data structures and algorithms guide for the full path.
Practice: write the algorithm first
For each problem, write the steps in plain words, trace them on one example, then code them:
- Check whether a number is even or odd.
- Find the sum of numbers from 1 to N.
- Swap two numbers.
- Count the vowels in a word.
- Find the smallest value in a list.
Most of these need a loop. If loops are still new, read loops in programming first. Writing the algorithm before the code is a habit that helps in every coding test and interview.
Frequently Asked Questions
What is an algorithm in simple words?
What is the difference between an algorithm and a program?
What is the difference between an algorithm and a flowchart?
What are the properties of a good algorithm?
Which algorithms should a beginner learn first?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

