Algorithm is a set of finite, well-defined steps or instructions designed to solve a problem or perform a computation. It can also be defined as a procedure for solving a mathematical or computational problem in a finite number of steps, often involving repetitive or recursive operations.
- Solve complex problems efficiently and effectively.
- Automate tasks to make processes faster and reliable.
- Enable computers to perform complex and repetitive tasks.
Use of the Algorithms
Algorithms are fundamental in solving problems efficiently across various fields:
- Computer Science: Basis of programming, from simple sorting and searching to AI and machine learning.
- Mathematics: Solve problems like linear equations, shortest paths, and optimization.
- Operations Research: Optimize logistics, transportation, and resource allocation.
- Artificial Intelligence: Power intelligent systems for tasks like image recognition, NLP, and decision-making.
- Data Science: Analyze and extract insights from large datasets in marketing, healthcare, finance, etc.
Algorithms can be simple or complex, depending on the task. Think of it like following a recipe: step-by-step instructions lead to the desired outcome.
Properties of an Algorithm
- Each step should be clear and unambiguous.
- Inputs should be clearly defined and specified.
- Outputs should be clearly defined and specified.
- The algorithm must terminate after finite steps.
- Each step should be feasible and executable.
- The same input should always produce the same output.
- The algorithm should be independent of language.
How to Express an Algorithm?
- Natural Language: Describes the algorithm using simple, plain-English instructions.
- Flowchart: Represents the algorithm graphically using standard symbols and flow lines.
- Pseudocode: Describes the logic using code-like instructions without language-specific syntax.
Steps to Design an Algorithm
To design an algorithm, the following prerequisites must be considered:
- Problem Definition: Clearly define the problem to be solved.
- Constraints: Identify any limitations or rules.
- Inputs: Determine what data will be provided.
- Outputs: Specify the expected results.
- Solution Feasibility: Ensure the solution works within the given constraints.
Example: Consider the example find the Largest of Three Numbers.
Step 1: Fulfilling Prerequisites
- Problem: Find the largest of three numbers.
- Constraints: Only numeric inputs are allowed.
- Inputs: Three numbers (num1, num2, num3).
- Output: The largest number among the three.
- Solution: Compare the numbers using conditional statements to determine the largest.
Step 2: Designing the algorithm
Now let's design the algorithm with the help of the above pre-requisites:
START
1. Read three numbers: num1, num2, num3
2. If num1 > num2 and num1 > num3, then
largest = num1
Else if num2 > num3, then
largest = num2
Else
largest = num3
3. Print largest
END
Step 3: Implementation
Program:
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3, largest;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 > num2 && num1 > num3)
largest = num1;
else if (num2 > num3)
largest = num2;
else
largest = num3;
cout << "Largest number: " << largest << endl;
return 0;
}
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int largest;
if (num1 > num2 && num1 > num3)
largest = num1;
else if (num2 > num3)
largest = num2;
else
largest = num3;
System.out.println("Largest number: " + largest);
sc.close();
}
}
# Example inputs
num1 = 10
num2 = 25
num3 = 15
if num1 > num2 and num1 > num3:
largest = num1
elif num2 > num3:
largest = num2
else:
largest = num3
print("Largest number:", largest)
Output
Largest number: 25Time Complexity: O(1)
- The algorithm performs a fixed number of comparisons, regardless of the input values, so the execution time is constant.
Auxiliary Space: O(1)
- Only a fixed number of variables are used (
num1,num2,num3,largest), so memory usage is constant.
Note: A problem can have multiple algorithmic solutions. For example, finding the largest of three numbers can be solved using if-else, Python’s max() function, ternary operators, or sorting. The choice of method depends on readability, simplicity, and performance considerations.
How to Analyze an Algorithm
To determine if an algorithm is efficient, its performance is analyzed in two ways:
1. Priori Analysis (Before Implementation):
- Evaluates the algorithm theoretically, without running it.
- Assumes factors like processor speed are constant.
- Provides an approximate measure of time and space complexity.
2. Posterior Analysis (After Implementation):
- Evaluates the algorithm practically, by executing it.
- Measures real performance: correctness, time taken, and memory used.
- Dependent on hardware and compiler.
For more, refer to the Complete Data Structures & Algorithms Tutorial