Programming BasicsBasicsbeginner

What Is an Algorithm? Simple Examples

Algorithm meaning with everyday examples, how to write the steps, draw a flowchart, and turn it into a small program.

Published
TL;DR – Quick Answer

An algorithm is a finite list of clear steps that solves a problem, such as a recipe or the steps to find the largest of three numbers. You write the steps in plain words, check them with a flowchart or a trace, and then turn them into code in any programming language.

On This Page

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:

  1. Start.
  2. Pour one cup of water into a pan.
  3. Heat the water until it boils.
  4. Add tea powder and sugar.
  5. Boil for two minutes.
  6. Add milk and boil for one more minute.
  7. Strain the tea into a cup.
  8. 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 Print 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.

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:

  1. Check whether a number is even or odd.
  2. Find the sum of numbers from 1 to N.
  3. Swap two numbers.
  4. Count the vowels in a word.
  5. 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?
It is a step-by-step method to solve a problem. A recipe, directions to a friend's house and the steps to withdraw cash from an ATM are all algorithms.
What is the difference between an algorithm and a program?
An algorithm is the plan, written in plain words or pseudocode, and it does not depend on any language. A program is that plan written in a real language such as Java or Python so a computer can run it.
What is the difference between an algorithm and a flowchart?
An algorithm lists the steps in words. A flowchart shows the same steps as a picture with shapes and arrows. Both describe the same logic.
What are the properties of a good algorithm?
It has clear input and output, every step is precise, it finishes after a finite number of steps, and it gives the correct answer. Good algorithms are also efficient.
Which algorithms should a beginner learn first?
Start with linear search, finding the largest value, swapping two numbers, and simple sorting such as bubble sort. Then learn binary search and recursion.

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

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us