Best Case ,Worst Case and Average Case Analysis of Algorithms

 

📌 1. Best Case Analysis

  • Definition: The minimum number of steps an algorithm takes on any input of size n.

  • It tells us the fastest the algorithm can complete.

  • Usefulness: Often not practical, because it may rarely happen.

  • Notation: Ω (Omega) is used to describe the lower bound.

  • Example (Linear Search):

    • Search for the first element in an array: only 1 comparison.

    • Best case time = Ω(1)


📌 2. Worst Case Analysis

  • Definition: The maximum number of steps the algorithm takes on any input of size n.

  • It shows the slowest performance scenario.

  • Usefulness: Very important in real-time and critical systems where guarantees are needed.

  • Notation: O (Big O) is used to describe the upper bound.

  • Example (Linear Search):

    • Search for a value not present or at the last position: n comparisons.

    • Worst case time = O(n)


📌 3. Average Case Analysis

  • Definition: The expected number of steps over all possible inputs, assuming a known distribution of inputs.

  • Tells us the typical performance of the algorithm.

  • Usefulness: Most realistic for general-purpose programs.

  • Notation: Θ (Theta) is often used if average is also tight.

  • Example (Linear Search):

    • On average, the element is found halfway: n/2 comparisons.

    • Average case time = Θ(n)


📊 Comparison Table

CaseMeaningExample (Linear Search)Time Notation
Best Case    Fastest case (ideal input)    Element at index 0    Ω(1)
Worst Case    Slowest case (bad input)    Element not present    O(n)
Average Case    Expected steps over all inputs    Element at middle    Θ(n)

🔍 Binary Search: Overview

Binary Search is used to search a sorted array by repeatedly dividing the search interval in half.


1. Best Case

  • What happens: The target element is found at the middle of the array in the first step.

  • Steps required: 1 comparison.

  • Time Complexity:

    Best case=Ω(1)\text{Best case} = \Omega(1)

📌 Example:
Search for 50 in [10, 20, 30, 40, 50, 60, 70]
Middle element = 50 → Found immediately!


2. Worst Case

  • What happens: The element is not present, or found at one of the far ends after all divisions.

  • Steps required: Keep dividing until sub-array becomes size 1.

  • Time Complexity:

    Worst case=O(log2n)\text{Worst case} = O(\log_2 n)

📌 Example:
Search for 5 in [10, 20, 30, 40, 50, 60, 70]
Requires ~3 comparisons:

  • Check 40 → go left

  • Check 20 → go left

  • Check 10 → not found


📊 3. Average Case

  • What happens: On average, the element is found in the middle of the recursive calls.

  • Time Complexity:

    Average case=Θ(log2n)\text{Average case} = Θ(\log_2 n)
  • The average number of comparisons is approximately:

    log2n1\log_2 n - 1

📌 Summary Table for Binary Search

CaseDescription    Time Complexity
Best Case            Element found in first comparison            Ω(1)
Worst Case            Element not found or last to be checked            O(log n)
Average Case            Typical number of comparisons            Θ(log n)

Comments

Popular posts from this blog

Data Structures and Algorithms PCCST303 KTU 2024 Scheme- Dr Binu V P

Performance Analysis - Time and Space Complexity

Data Structures and Data Abstraction