Insertion Sort is a simple yet effective comparison-based sorting algorithm widely used for small datasets and nearly sorted arrays. It works similarly to sorting playing cards in your hand—by placing each new card (element) in its correct position.
In this article, we will cover:
Before diving in, you may want to review Bubble Sort and Selection Sort, as they follow similar iterative approaches.
Insertion Sort builds the final sorted array one element at a time by:
This process continues until all elements are correctly placed.
Let’s take an example array:
Input Array: [7, 1, 23, 4, 19]
Iteration | Array State | Explanation |
---|---|---|
Start | [7, 1, 23, 4, 19] | Initial unsorted array |
1st Iteration | [1, 7, 23, 4, 19] | 1 is inserted before 7 |
2nd Iteration | [1, 7, 23, 4, 19] | 23 stays in place (already sorted) |
3rd Iteration | [1, 4, 7, 23, 19] | 4 is placed in the correct position |
4th Iteration | [1, 4, 7, 19, 23] | 19 is inserted before 23 |
At the end of these iterations, the array is sorted: [1, 4, 7, 19, 23]
.
for i = 1 to n-1
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key
arr[j + 1] = arr[j] // Shift elements
j = j - 1
arr[j + 1] = key // Insert key at correct position
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {7, 1, 23, 4, 19};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {7, 1, 23, 4, 19};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
class InsertionSort {
static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
static void printArray(int[] arr) {
for (int num : arr)
System.out.print(num + " ");
System.out.println();
}
public static void main(String[] args) {
int[] arr = {7, 1, 23, 4, 19};
insertionSort(arr);
System.out.println("Sorted array: ");
printArray(arr);
}
}
Case | Time Complexity |
---|---|
Best Case (Already Sorted) | O(n) |
Worst Case (Reverse Sorted) | O(n²) |
Average Case | O(n²) |
Why?
Space Complexity: O(1) (In-place sorting algorithm)
Sorting Algorithm | Best Case | Worst Case | Stability | Space Complexity |
---|---|---|---|---|
Insertion Sort | O(n) | O(n²) | ✅ Stable | O(1) |
Bubble Sort | O(n) | O(n²) | ✅ Stable | O(1) |
Selection Sort | O(n²) | O(n²) | ❌ Not Stable | O(1) |
Merge Sort | O(n log n) | O(n log n) | ✅ Stable | O(n) |
QuickSort | O(n log n) | O(n²) | ❌ Not Stable | O(log n) |
Insertion Sort is a simple and efficient sorting algorithm for small datasets or nearly sorted arrays. While it is slower than Merge Sort and QuickSort, it has the advantage of being an in-place, stable sorting algorithm with O(1) space complexity.
When to Use Insertion Sort?
- When the dataset is small.
- When the array is partially sorted.
- When stable sorting is required.