📄 Need a professional CV? Try our Resume Builder! Get Started

Understanding Insertion Sort: A Step-by-Step Guide 📊

Ever wondered how playing cards are sorted in your hands? You probably pick up one card at a time and place it in the right position among the cards you're already holding. That's exactly how Insertion Sort works! In this blog, we'll explore this intuitive sorting algorithm and break it down into simple, easy-to-understand steps.

Date: Sun Jan 5, 2025

Watch the Video Tutorial 🎥

For a visual explanation of the Insertion Sort algorithm, check out this detailed video tutorial:

What is Insertion Sort? 🤔

Insertion Sort is like organizing a hand of cards as you receive them. Imagine you're dealing cards one by one. With each new card, you place it in the correct position among the cards you already have. This simple but effective method is exactly what makes Insertion Sort so intuitive!

Real-World Example: Organizing Library Books 📚

Think of a librarian arranging books on a shelf. They:

  1. Pick up one new book at a time
  2. Compare it with books already on the shelf
  3. Slide other books over to make space
  4. Place the new book in its correct spot

How Does Insertion Sort Work? Step-by-Step Guide 📝

Let's sort this list: [9, 5, 1, 4, 3]

Initial Array: [9, 5, 1, 4, 3]

Step 1: Compare 5 with 9
[5, 9, 1, 4, 3]

Step 2: Compare 1 with sorted portion
[1, 5, 9, 4, 3]

Step 3: Place 4 in correct position
[1, 4, 5, 9, 3]

Step 4: Finally, place 3
[1, 3, 4, 5, 9]

Final Sorted Array: [1, 3, 4, 5, 9]

Insertion Sort: Visual Step-by-Step Guide

Sorted
Current Element
Comparing

Initial Array

9
5
1
4
3
Starting with unsorted array [9, 5, 1, 4, 3]

Step 1: Insert 5

5
9
1
4
3
Compare 5 with 9, swap them since 5 < 9

Step 2: Insert 1

1
5
9
4
3
1 moves to the beginning since it's smaller than both 5 and 9

Step 3: Insert 4

1
4
5
9
3
4 is inserted between 1 and 5

Final Step: Insert 3

1
3
4
5
9
3 is inserted between 1 and 4 to complete the sort

Algorithm Summary

  • Time Complexity: O(n²) in worst and average cases
  • Space Complexity: O(1)
  • Best Case: O(n) when array is already sorted
  • Stable sorting algorithm
  • In-place algorithm

Python Implementation 🐍

def insertion_sort(arr):
    # Start from the second element
    for i in range(1, len(arr)):
        # Store the current element
        current_element = arr[i]
        
        # Find where to insert current element
        position = i - 1
        while position >= 0 and arr[position] > current_element:
            # Shift larger elements to the right
            arr[position + 1] = arr[position]
            position -= 1
        
        # Insert the element in its correct position
        arr[position + 1] = current_element
        
        # Print step-by-step progress
        print(f"Step {i}: {arr}")
    
    return arr

# Example usage with our list
numbers = [9, 5, 1, 4, 3]
print(f"Original array: {numbers}")
sorted_numbers = insertion_sort(numbers)
print(f"Sorted array: {sorted_numbers}")

When Should You Use Insertion Sort? 🎯

Perfect For:

  • Small lists (less than 50 items)
  • Nearly sorted data
  • When you need a simple implementation
  • When memory space is limited

Not Ideal For:

  • Large datasets
  • When quick performance is crucial
  • Completely random or reversed data

Practice Exercise 📝

Try sorting this array by hand: [7, 2, 8, 1, 6]

  1. Start with [7]
  2. Insert each number one by one
  3. Track your steps
  4. Compare your result with the algorithm's output

Time Complexity Explained Simply ⏱️

- Best Case: O(n) - When the list is already sorted
- Average Case: O(n²) - For typical random data
- Worst Case: O(n²) - When the list is reverse sorted

In simple terms: For a list of 100 items, you might need up to 10,000 steps in the worst case!