What Is an Algorithm? A Complete Beginner’s Guide to Algorithms in Programming
Before writing a Python program, it is useful to know what steps the program needs to perform.
Imagine someone asks you:
Write a program to find the largest number among three numbers.
You could immediately start writing Python code. But there is another, more organized approach.
First, think about the steps:
Take three numbers.
Compare the numbers.
Find which number is the largest.
Display the largest number.
These step-by-step instructions form an algorithm.
Algorithms are one of the most important concepts in programming because they help us solve problems systematically before converting our solutions into code.
What Is an Algorithm?
An algorithm is a finite, ordered sequence of clear steps used to solve a problem or perform a task.
In simple words:
An algorithm is a step-by-step procedure for solving a problem.
An algorithm does not have to be written in a programming language.
It can be written using simple English, mathematical steps, or structured descriptions.
For example, an algorithm for making tea could be:
1. Boil water.
2. Add tea leaves.
3. Add milk.
4. Add sugar.
5. Boil for a few minutes.
6. Strain the tea.
7. Serve.
This is an algorithm because it describes a sequence of steps for completing a task.
Why Are Algorithms Important in Programming?
When solving a programming problem, writing code immediately is not always the best approach.
An algorithm helps you think about the solution first.
It can help you:
Understand the problem
Organize your thoughts
Identify required inputs
Identify required outputs
Decide what processing is needed
Avoid unnecessary code
Find logical mistakes
Convert the solution into a programming language
The basic process is:
Problem
↓
Understand the Problem
↓
Break the Problem into Smaller Parts
↓
Design an Algorithm
↓
Write the Program
↓
Test the Program
↓
Debug if Necessary
A Simple Example of an Algorithm
Suppose the problem is:
Find the sum of two numbers.
Before writing Python code, create the algorithm.
Algorithm
Step 1: Start
Step 2: Input the first number
Step 3: Input the second number
Step 4: Add the two numbers
Step 5: Display the result
Step 6: Stop
This gives us a clear plan.
Algorithm and Python Code
Once the algorithm is ready, it can be converted into Python.
Algorithm
1. Start
2. Read two numbers
3. Calculate their sum
4. Display the sum
5. Stop
Python program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
The Python program is simply an implementation of the algorithm.
Characteristics of a Good Algorithm
A useful algorithm generally has several important characteristics.
1. Input
An algorithm may require one or more inputs.
For example:
Enter length
Enter width
The values become the inputs of the algorithm.
2. Output
An algorithm should produce a result.
For example:
Area = 100
This is the output.
3. Definiteness
Each step should be clear and unambiguous.
For example:
Add A and B
is clear.
But:
Do something with A and B
is not clear enough.
4. Finiteness
An algorithm should eventually stop after a finite number of steps for the problem being solved.
For example:
1. Input two numbers
2. Add them
3. Display the result
4. Stop
The algorithm ends after completing its steps.
5. Effectiveness
Each step should be practical and possible to perform.
For example:
Read a number
Add two numbers
Display the result
are meaningful operations.
Algorithm for Finding the Area of a Rectangle
Consider the problem:
Write a program to calculate the area of a rectangle.
The formula is:
Area = Length × Width
Algorithm
Step 1: Start
Step 2: Input length
Step 3: Input width
Step 4: Calculate area = length × width
Step 5: Display area
Step 6: Stop
Python implementation
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area =", area)
Notice how the algorithm and program follow the same logical sequence.
Algorithm for Checking Even or Odd
Suppose the problem is:
Determine whether a number is even or odd.
We can use the remainder operator %.
If a number is divisible by 2, it is even.
Algorithm
Step 1: Start
Step 2: Input a number
Step 3: Divide the number by 2 and find the remainder
Step 4: If the remainder is 0, display "Even"
Step 5: Otherwise, display "Odd"
Step 6: Stop
Python program
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
The algorithm tells us what to do.
Python tells the computer how to perform it.
Algorithm for Finding the Largest of Two Numbers
Problem:
Find the largest of two numbers.
Algorithm
Step 1: Start
Step 2: Input A and B
Step 3: Compare A and B
Step 4: If A is greater than B, display A
Step 5: Otherwise, display B
Step 6: Stop
Python program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Largest =", a)
else:
print("Largest =", b)
Algorithm for Calculating Simple Interest
Suppose we want to calculate simple interest.
The formula is:
SI = (P × R × T) / 100
where:
P = Principal
R = Rate of interest
T = Time
Algorithm
Step 1: Start
Step 2: Input principal P
Step 3: Input rate R
Step 4: Input time T
Step 5: Calculate SI = (P × R × T) / 100
Step 6: Display SI
Step 7: Stop
Python program
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))
si = (p * r * t) / 100
print("Simple Interest =", si)
Algorithm for Finding the Average of Three Numbers
Problem:
Find the average of three numbers.
Algorithm
Step 1: Start
Step 2: Input three numbers A, B and C
Step 3: Calculate total = A + B + C
Step 4: Calculate average = total / 3
Step 5: Display average
Step 6: Stop
Python program
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
total = a + b + c
average = total / 3
print("Average =", average)
Algorithm for Finding the Largest of Three Numbers
This problem requires multiple comparisons.
Problem
Find the largest number among three numbers.
Algorithm
Step 1: Start
Step 2: Input A, B and C
Step 3: Compare A with B and C
Step 4: If A is greater than both, A is the largest
Step 5: Otherwise compare B with C
Step 6: If B is greater than C, B is the largest
Step 7: Otherwise C is the largest
Step 8: Display the largest number
Step 9: Stop
The algorithm gives us a logical plan before we write the Python code.
Algorithm for Finding Factorial
The factorial of a positive integer n is:
n! = n × (n-1) × (n-2) × ... × 1
For example:
5! = 5 × 4 × 3 × 2 × 1
= 120
Algorithm
Step 1: Start
Step 2: Input n
Step 3: Set factorial = 1
Step 4: Repeat from 1 to n
Step 5: Multiply factorial by the current number
Step 6: Display factorial
Step 7: Stop
Later, this algorithm can be implemented using Python loops.
Algorithm for a Multiplication Table
Suppose we want to print the multiplication table of 5.
Algorithm
Step 1: Start
Step 2: Input a number
Step 3: Set counter = 1
Step 4: Multiply the number by the counter
Step 5: Display the result
Step 6: Increase counter
Step 7: Repeat until counter reaches 10
Step 8: Stop
This example shows that algorithms can also contain repetition.
Algorithms Can Contain Decisions
Some problems require decisions.
For example:
Check whether a student has passed or failed.
Suppose the passing percentage is 40%.
Algorithm
Step 1: Start
Step 2: Input percentage
Step 3: If percentage >= 40, display "Pass"
Step 4: Otherwise, display "Fail"
Step 5: Stop
The decision can be represented as:
Percentage
|
>= 40?
/ \
Yes No
| |
Pass Fail
This type of decision will later be implemented using Python conditional statements.
Algorithms Can Contain Repetition
Some problems require repeating an operation.
For example:
Print numbers from 1 to 10.
Algorithm
Step 1: Start
Step 2: Set number = 1
Step 3: Display number
Step 4: Increase number by 1
Step 5: Repeat steps 3 and 4 while number <= 10
Step 6: Stop
Later, this can be implemented using Python loops such as for and while.
Algorithm vs Program
An algorithm and a program are related, but they are not the same.
| Algorithm | Program |
|---|---|
| Step-by-step solution | Actual implementation |
| Usually language-independent | Written in a programming language |
| Focuses on logic | Focuses on implementation |
| Can be written in simple language | Follows programming syntax |
| Created before coding | Created by implementing the algorithm |
For example:
Algorithm
↓
Step-by-step solution
↓
Python
↓
Program
Algorithm vs Flowchart
Algorithms and flowcharts both describe solutions, but they use different formats.
Algorithm
Uses written steps.
1. Start
2. Input number
3. Check number
4. Display result
5. Stop
Flowchart
Uses graphical symbols to represent the process.
Start
↓
Input
↓
Process
↓
Output
↓
Stop
Flowcharts will be discussed separately in the upcoming part of the course.
How to Write an Algorithm
When you are given a programming problem, follow these steps.
Step 1: Understand the problem
Know exactly what is being asked.
Step 2: Identify the input
Determine what information the program needs.
Step 3: Identify the output
Determine what result needs to be produced.
Step 4: Determine the processing
Decide what calculations, comparisons, or operations are required.
Step 5: Write the steps in order
Arrange the solution logically.
Step 6: Check the algorithm
Try the steps using sample values.
Step 7: Convert the algorithm into code
Once the logic is correct, implement it in Python.
Example: Complete Problem-Solving Process
Let's solve a simple problem from beginning to end.
Problem
Write a program to calculate the total price of three products.
Suppose the prices are:
₹100
₹250
₹150
Step 1: Identify Input
We need three prices.
Price 1
Price 2
Price 3
Step 2: Identify Processing
Add the three prices.
Total = Price1 + Price2 + Price3
Step 3: Identify Output
Display the total.
Step 4: Write the Algorithm
Step 1: Start
Step 2: Input price1
Step 3: Input price2
Step 4: Input price3
Step 5: Calculate total = price1 + price2 + price3
Step 6: Display total
Step 7: Stop
Step 5: Write Python Code
price1 = float(input("Enter price of product 1: "))
price2 = float(input("Enter price of product 2: "))
price3 = float(input("Enter price of product 3: "))
total = price1 + price2 + price3
print("Total price =", total)
The algorithm made the coding process straightforward.
How to Check Whether an Algorithm Is Correct
Before writing code, test your algorithm manually.
Suppose:
A = 10
B = 20
For the largest-number algorithm:
Compare 10 and 20
20 is greater
Therefore, largest = 20
This manual checking process is often called a dry run.
Dry runs help you find logical problems before or while writing the actual program.
Common Mistakes While Writing Algorithms
Mistake 1: Missing a step
Every required operation should be included.
Mistake 2: Steps are unclear
Each step should have a specific meaning.
Mistake 3: Wrong order
Steps should appear in the correct logical sequence.
Mistake 4: Ignoring special cases
Some problems have special situations that need to be considered.
Mistake 5: Writing programming syntax instead of an algorithm
An algorithm should focus on the solution logic rather than the syntax of a particular language.
Why Algorithms Matter in AI
Algorithms are not limited to basic programming.
They are fundamental to artificial intelligence and machine learning as well.
For example, an AI application may involve steps such as:
Collect Data
↓
Clean Data
↓
Prepare Data
↓
Train Model
↓
Test Model
↓
Evaluate Results
↓
Make Predictions
Machine learning itself involves algorithms that learn patterns from data.
As you continue through the Python with AI course, you will encounter many different algorithms and understand how they are implemented.
A Simple Rule to Remember
Whenever you receive a programming problem, remember:
Understand
↓
Break Down
↓
Design Algorithm
↓
Write Code
↓
Test
↓
Debug
Do not be afraid of large programming problems.
Start by understanding the problem and then create a clear sequence of steps.
Key Takeaways
An algorithm is a step-by-step procedure for solving a problem.
Algorithms are usually designed before writing code.
A good algorithm should have clear and ordered steps.
Algorithms can involve input, processing, output, decisions, and repetition.
Algorithms are different from programs.
Algorithms help programmers organize their logic.
Algorithms can be tested using sample values and dry runs.
The same algorithm can often be implemented in different programming languages.
Algorithms are important in software development, Python, AI, and machine learning.
Frequently Asked Questions
What is an algorithm in simple words?
An algorithm is a sequence of steps used to solve a problem or complete a task.
Is an algorithm a program?
No. An algorithm describes the solution, while a program implements that solution using a programming language.
Do algorithms have to be written in Python?
No. Algorithms are generally independent of a particular programming language.
Why should I create an algorithm before coding?
It helps you understand the logic and reduces the chance of making mistakes while writing the program.
Can an algorithm contain conditions?
Yes. Algorithms can contain decisions such as if, otherwise, or similar logical conditions.
Can an algorithm contain loops?
Yes. An algorithm can describe repeated operations.
Are algorithms important for AI?
Yes. Algorithms are fundamental to programming and are also central to artificial intelligence and machine learning.
Comments
Post a Comment