DSA Insertion Sort

DSA Tutorial

DSA Insertion Sort

Insertion Sort is one of the most intuitive sorting algorithms.
It works the same way we sort cards in our hand—by taking one card at a time and inserting it into the correct position.

 What is Insertion Sort?

The Insertion Sort algorithm:

  • Uses two parts of the array

    • Sorted part (left side)

    • Unsorted part (right side)

  • Takes one value at a time from the unsorted part

  • Inserts it into the correct position in the sorted part

  • Repeats until the array is fully sorted

 The first element is always considered already sorted.


 Speed (In Simple Words)

  • Fast for small arrays

  • Very efficient for nearly sorted data

  • Slow for large, random arrays


How Insertion Sort Works (Step Logic)

  1. Take the first value → consider it sorted

  2. Pick the next value from the unsorted part

  3. Compare it with values in the sorted part

  4. Shift larger values one position to the right

  5. Insert the value at the correct position

  6. Repeat for all remaining values


Manual Run-Through (Step by Step)

Initial Array

[7, 12, 9, 11, 3]

Step 1: First element is sorted

[7 | 12, 9, 11, 3]

Step 2: Insert 12

12 > 7 → already in correct place

[7, 12 | 9, 11, 3]

Step 3: Insert 9

Move 9 between 7 and 12

[7, 9, 12 | 11, 3]

Step 4: Insert 11

Move 11 between 9 and 12

[7, 9, 11, 12 | 3]

Step 5: Insert 3

3 is smallest → move to front

[3, 7, 9, 11, 12]

 Final Sorted Array

[3, 7, 9, 11, 12]

What Happened in the Manual Run?

✔ First element assumed sorted
✔ Each new element compared only with sorted part
✔ Sorted part grows from left to right
✔ Unsorted part shrinks each pass
✔ Total passes = n − 1 (for n elements)


Insertion Sort Algorithm (Conceptual)

  • Outer loop → selects next element to insert

  • Inner loop → finds correct position in sorted part

  • Elements larger than the key are shifted, not swapped


Basic Insertion Sort (Simple Logic – Python Style)


 

 Easy to understand but internally expensive due to hidden memory shifts.


Why This Can Be Slow (Important Concept)

In high-level languages (Python, JS):

  • remove → shifts many elements left

  • insert → shifts many elements right

  • These hidden memory shifts cost time

 You don’t see them, but the computer still does the work.


Optimized Insertion Sort (Efficient Version)

Instead of removing and inserting:

  • Store the value

  • Shift only required elements

  • Insert once


 

 Why This Is Better

✔ Fewer memory shifts
✔ Breaks early when correct position is found
✔ Used in real implementations


Insertion Sort Time Complexity

Insertion Sort works on n elements.

Comparisons

  • On average, each element compares with about n/2 elements

  • Loop runs approximately n times

Final Complexity

O(n × n) = O(n²)

⏱ Time Complexity Table

CaseConditionTime Complexity
Best CaseAlready sortedO(n)
Average CaseRandom orderO(n²)
Worst CaseReverse sortedO(n²)

📌 Best case is fast because no shifting is required.


Space Complexity

MetricValue
Extra SpaceO(1)
TypeIn-place

Key Properties (Exam Gold)

✔ Stable sorting algorithm
✔ Adaptive (responds to existing order)
✔ In-place
✔ Efficient for small / nearly sorted arrays
❌ Not suitable for large datasets


✅ Final Summary

✔ Insertion Sort builds a sorted array gradually
✔ Works like sorting playing cards
✔ Very intuitive and easy to implement
✔ Optimized version reduces memory shifts
✔ Best case: O(n)
✔ Worst case: O(n²)
✔ Excellent for learning and interviews

You may also like...