Insertion Sort
Insertion Sort is a simple, intuitive sorting algorithm that builds the final sorted array one element at a time. It works similarly to how people sort playing cards in their hands - picking each card and inserting it into its correct position among the already sorted cards. Despite its O(n²) time complexity, Insertion Sort is efficient for small datasets and nearly sorted arrays, making it practical for real-world applications.
Visualization
Interactive visualization for Insertion Sort
Animation Speed: 400ms
🟡 Yellow: Current | 🔵 Blue: Comparing | 🟢 Green: Sorted
Interactive visualization with step-by-step execution
Implementation
1function insertionSort(arr: number[]): number[] {
2 const sorted = [...arr];
3
4 for (let i = 1; i < sorted.length; i++) {
5 const key = sorted[i];
6 let j = i - 1;
7
8 while (j >= 0 && sorted[j] > key) {
9 sorted[j + 1] = sorted[j];
10 j--;
11 }
12 sorted[j + 1] = key;
13 }
14 return sorted;
15}Deep Dive
Theoretical Foundation
Insertion Sort works by dividing the array into a sorted and an unsorted region. Initially, the first element is considered sorted. The algorithm picks the next element from the unsorted region and inserts it into its correct position in the sorted region by shifting larger elements to the right. This process continues until all elements are in the sorted region. The key insight is that at any point, the left portion of the array is always sorted. Insertion Sort is stable (maintains relative order of equal elements), in-place (requires O(1) extra space), and adaptive (performs better on partially sorted data with O(n) best case).
Complexity
Time
O(n)
O(n²)
O(n²)
Space
O(1)
Applications
Industry Use
Small dataset sorting in embedded systems
Hybrid sorting algorithms (TimSort, Introsort) for small partitions
Online sorting - adding elements to already sorted list
Nearly sorted data in real-time systems
Simple sorting needs where code simplicity matters
Educational demonstrations of sorting concepts
Use Cases
Related Algorithms
Quicksort
A highly efficient, in-place sorting algorithm that uses divide-and-conquer strategy. Invented by Tony Hoare in 1959, it remains one of the most widely used sorting algorithms due to its excellent average-case performance and cache efficiency.
Merge Sort
A stable, divide-and-conquer sorting algorithm with guaranteed O(n log n) performance.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire array is sorted. Named for the way larger elements 'bubble' to the top (end) of the array.
Selection Sort
Selection Sort is a simple comparison-based sorting algorithm that divides the array into sorted and unsorted regions. It repeatedly selects the smallest (or largest) element from the unsorted region and moves it to the end of the sorted region. The algorithm performs the minimum number of swaps (n-1 at most), making it useful when memory writes are expensive operations.