Skip to content
Home Β» Algorithms

Algorithms

Below is a clear, complete, exam-oriented explanation of Algorithms, written exactly like a Data Structures & Algorithms expert.


πŸ“˜ Introduction to Algorithms

An Algorithm is a finite, step-by-step, logical procedure used to solve a problem or perform a computation.

In simple words:

πŸ‘‰ Algorithm = A set of instructions to achieve a specific task efficiently.

Examples of algorithms in daily life:

  • Steps to prepare tea
  • Steps to search a contact in your phone
  • Steps to arrange books in order

In computer science, algorithms operate on data structures.


✨ Characteristics of a Good Algorithm

A good algorithm must have:

1. Input

Should take zero or more inputs.

2. Output

Should produce at least one output.

3. Definiteness

Each step must be clear and unambiguous.

4. Finiteness

Algorithm must terminate after a finite number of steps.

5. Effectiveness

All operations must be basic and feasible.

6. Efficiency

Should use minimum time and memory.


πŸ“‚ Classification / Types of Algorithms

There are various ways to classify algorithms. Below is the most important classification for exams and interviews:


1. Brute Force Algorithms

  • Check all possible solutions
  • Simple but inefficient
    Example: Linear search, checking all permutations

2. Divide and Conquer Algorithms

Split β†’ Solve β†’ Combine
Used for problems where the solution can be broken into subproblems.

Examples:

  • Merge Sort
  • Quick Sort
  • Binary Search

3. Greedy Algorithms

Make the locally optimal choice at every step hoping to reach a global optimum.

Examples:

  • Kruskal’s algorithm (MST)
  • Prim’s algorithm (MST)
  • Dijkstra’s shortest path
  • Fractional knapsack

4. Dynamic Programming Algorithms (DP)

Solve complex problems by breaking them into overlapping subproblems and using memoization or tabulation.

Examples:

  • Fibonacci (DP version)
  • 0/1 Knapsack
  • Longest Common Subsequence (LCS)

5. Backtracking Algorithms

Try all solutions but backtrack when a solution is not valid.

Examples:

  • N-Queens problem
  • Sudoku solving
  • Graph coloring

6. Branch and Bound Algorithms

Used to find optimal solutions by pruning branches that cannot produce a better solution.

Examples:

  • Traveling Salesman Problem (TSP)
  • 0/1 Knapsack (optimal)

7. Randomized Algorithms

Use randomness to make decisions during execution.

Examples:

  • Randomized Quick Sort
  • Monte Carlo algorithms

8. Recursive Algorithms

Functions that call themselves until the base condition is met.

Examples:

  • Factorial
  • Tower of Hanoi
  • Tree traversal

9. Iterative Algorithms

Use loops instead of recursion.

Examples:

  • Iterative Fibonacci
  • Iterative factorial

πŸ“Œ Time and Space Complexity

Algorithms are analyzed based on:

1. Time Complexity

How much time an algorithm takes.

Common notations:

  • O(1) – constant time
  • O(log n) – logarithmic
  • O(n) – linear
  • O(n log n) – log linear
  • O(nΒ²) – quadratic
  • O(2ⁿ) – exponential
  • O(n!) – factorial

2. Space Complexity

How much memory an algorithm uses.

Includes:

  • Input space
  • Auxiliary space (extra memory)

πŸ“˜ Steps to Design an Algorithm

  1. Understand the problem
  2. Identify input/output
  3. Write logical steps
  4. Choose best data structures
  5. Analyze time & space
  6. Optimize if required
  7. Correctness proof & testing

πŸ“Œ Example of an Algorithm

Problem: Find the largest element in an array

Algorithm:

  1. Start
  2. Assume the first element is max
  3. Repeat for each element
    • If current element > max
    • Update max
  4. Print max
  5. Stop

Time Complexity: O(n)
Space Complexity: O(1)


πŸ“˜ Difference Between Data Structures and Algorithms

Data StructureAlgorithm
How data is storedHow data is processed
Structure-orientedProcedure-oriented
Example: Array, Stack, TreeExample: sorting, searching
Focus on memoryFocus on computation

They are complementary:
πŸ‘‰ Algorithm uses data structure to work efficiently.


🎯 Summary (for exams)

An Algorithm is a finite sequence of unambiguous instructions designed to solve a problem effectively.
Types include:

  • Brute Force
  • Divide & Conquer
  • Greedy
  • Dynamic Programming
  • Backtracking
  • Branch & Bound
  • Randomized
  • Recursive & Iterative

Algorithms are analyzed using time and space complexity.