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.
Here’s how insertion sort behaves:
- Inner Loop: The inner
whileloop checks if the current element is greater than the previous one. Since the list is already sorted, this conditionarr[j] > keyis alwaysFalseon the first comparison for each element, and the loop does not run. - Number of Operations: For each element, the inner loop performs one comparison and then exits, taking constant time
for each element.
Mathematically, the total number of comparisons is:
Since the time complexity for each of the
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.
-
Inner Loop: For each element, the inner
whileloop must shift all previous elements to make space. This means for the second element, one comparison and shift occur; for the third, two comparisons and shifts; and so forth. -
Number of Operations: The first element requires 0 comparisons, the second requires 1, the third requires 2, and so on, up to
comparisons for the last element.
The total number of comparisons and shifts in the worst case is:
This sum is approximately
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.
- Inner Loop: For each insertion, the inner
whileloop is expected to run about half as many times as in the worst case. - Number of Operations: For each element, we perform approximately
comparisons, where is the current position of the element in the loop.
The total number of comparisons is roughly:
Ignoring constant factors, the average-case time complexity is
Summary of Time Complexity for Insertion Sort
| Case | Time Complexity |
|---|---|
| Best Case | |
| Average Case | |
| Worst Case |
In the next lesson, Common Time Complexities, we’ll explore typical time complexities like
🤖 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.
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.
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.
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.
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.