Knowledge Guide
HomeDSAFoundations

Best, Worst, and Average Cases

Let’s build on our analysis of insertion sort from the previous lesson. We’ve already covered the general time complexity of insertion sort. In this lesson, we’ll analyze how insertion sort performs in the best, average, and worst cases by calculating time complexity for each scenario.

Insertion Sort: Case Analysis

Insertion sort works by gradually building a sorted section of the list by inserting each new element into its correct position. The time it takes for each insertion depends on where the element needs to be placed, making the input order critical to the algorithm’s performance.

1. Best Case: Already Sorted List

In the best case, the input list is already sorted.

Image
Image

Here’s how insertion sort behaves:

Mathematically, the total number of comparisons is:

Since the time complexity for each of the elements is constant, the best-case time complexity of insertion sort is .

2. Worst Case: Reverse Sorted List

In the worst case, the input list is sorted in reverse order, meaning every element must be moved to the beginning of the sorted section.

Image
Image

The total number of comparisons and shifts in the worst case is:

This sum is approximately . Thus, the worst-case time complexity of insertion sort is .

3. Average Case: Randomly Ordered List

In the average case, we assume that the input list is randomly ordered. The number of comparisons for each element depends on where it needs to be inserted within the sorted section. On average, each element is compared with half of the elements in the sorted portion.

Image
Image

The total number of comparisons is roughly:

Ignoring constant factors, the average-case time complexity is .

Summary of Time Complexity for Insertion Sort

CaseTime Complexity
Best Case
Average Case
Worst Case

In the next lesson, Common Time Complexities, we’ll explore typical time complexities like , , and , and how these appear across various algorithms.

🤖 Don't fully get this? Learn it with Claude

Stuck on Best, Worst, and Average Cases? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Best, Worst, and Average Cases** (DSA) and want to truly understand it. Explain Best, Worst, and Average Cases from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Best, Worst, and Average Cases** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Best, Worst, and Average Cases** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Best, Worst, and Average Cases** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes