Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part B · Q10b3 marksCO1Module 4

Question 10b

Find the Best, Worst and average case time complexity of Insertion sort.

Topics: Insertion sort, Performance analysis: time and space complexity

Watch this exact question run

Loading visualizer…

Other parts: Worst case (reversed)

Solution

  • Best case — input already sorted: the inner while test fails immediately in every pass, so exactly n − 1 comparisons and 0 shifts → O(n).
  • Worst case — input in reverse order: pass i compares and shifts i elements, so 1 + 2 + … + (n − 1) = n(n − 1)/2 comparisons → O(n²).
  • Average case — each new key moves past about half of the sorted part on average: ≈ n²/4 comparisons → O(n²).
Measured by the engine for n = 6checked by code
CaseInputcomparisonsshifts
Best (sorted)1, 2, 3, 4, 5, 650
Worst (reversed)6, 5, 4, 3, 2, 11515
A random order4, 1, 6, 2, 5, 3117

How to write it in the exam

CaseInputComparisonsComplexity
Bestsortedn − 1O(n)
Averagerandom≈ n²/4O(n²)
Worstreverse sortedn(n − 1)/2O(n²)
How this answer was verified
  • tests/sorting.test.ts (comparison counts 5 and 15 for n = 6)