GoAlgo

GoAlgo

Insertion Sort

Sorting Algorithm

Insertion Sort

Insertion Sort builds the final sorted array one element at a time by inserting each new element into its correct position in the already sorted part.

No data to display.
Playback
Speed
Input
Comparisons:0
Swaps:0
Deep Dive

Understanding Insertion Sort in Detail

Insertion Sort is a simple, intuitive algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as Quick Sort, Heap Sort, or Merge Sort. However, it provides several advantages such as simple implementation and efficiency on small datasets.

Imagine you are playing cards. You pick up cards one by one from the table. When you pick up a new card, you look at the cards already in your hand (which are sorted) and insert the new card into the correct position. You shift the larger cards to the right to make space for the new one. This is exactly how Insertion Sort works.

The Logic Behind the Insertion

The core mechanism involves splitting the array into a "sorted" and "unsorted" section.

  • Split: The array is virtually split into a sorted part (left) and an unsorted part (right).
  • Pick: We take the first element from the unsorted part (the "key").
  • Shift: We compare the key with elements in the sorted part (moving right to left). If an element is larger than the key, we shift it one position to the right.
  • Insert: Once we find the correct spot (or reach the beginning), we insert the key.

Step-by-Step Algorithm Trace

Let's trace the algorithm on a small array.

Input Array: [12, 11, 13, 5, 6]

Pass 1:

  • Key: 11.
  • Compare 11 with 12. Since 11 < 12, shift 12 to the right.
  • Insert 11 at the start.

Result: [11, 12, 13, 5, 6] (11, 12 are sorted).

Pass 2:

  • Key: 13.
  • Compare 13 with 12. Since 13 > 12, no shift needed.

Result: [11, 12, 13, 5, 6] (11, 12, 13 are sorted).

Pass 3:

  • Key: 5.
  • Compare 5 with 13 (Shift 13). Compare 5 with 12 (Shift 12). Compare 5 with 11 (Shift 11).
  • Insert 5 at the start.

Result: [5, 11, 12, 13, 6].

Pass 4:

  • Key: 6.
  • Compare 6 with 13 (Shift). Compare 6 with 12 (Shift). Compare 6 with 11 (Shift). Compare 6 with 5 (Stop).
  • Insert 6 after 5.

Result: [5, 6, 11, 12, 13]. The array is sorted.

Code Implementation

Below is the implementation of Insertion Sort in multiple programming languages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements greater than key one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
C27 lines.c

Time & Space Complexity Analysis

Insertion Sort has unique performance characteristics that make it "adaptive."

Time Complexity

O(n²)Worst Case

If the array is in reverse order, every element must be compared and shifted all the way to the start.

O(n)Best Case

If the array is already sorted, the inner while loop never runs. The algorithm simply passes through the array once, making it incredibly fast for nearly-sorted data.

Space Complexity

O(1)Constant Space

It sorts in-place, requiring only one temporary variable for the key.

Interactive

Challenge Mode: Terminal Velocity

You've learned the theory. Now, can you insert faster than the loop? Test your reflexes and shifting logic in our new game!

When to Use Insertion Sort?

While simple, Insertion Sort is extremely useful in real-world scenarios where data is "almost" sorted.

Small Datasets

For very small arrays (e.g., fewer than 10-20 elements), Insertion Sort is often faster than Quick Sort or Merge Sort because it has lower overhead (no recursion stack, complex pivot logic).

Online Algorithms

Insertion Sort can sort a list as it receives it. If you are receiving data one piece at a time (like a live data feed) and need to maintain a sorted list, Insertion Sort is ideal because you simply "insert" the new data point into the correct spot without resorting the whole list.

Adaptive Sorting

If you know the data is already mostly sorted (e.g., adding a few new entries to a daily log file), Insertion Sort will run in near-linear time O(n), beating almost every other algorithm.