DECMATRIX

Mathematical Tools

Insertion Sort
i
Enter up to 21 integer numbers from -999 to 999, separated by commas.
5
0
1
-3
8
2
2
3
4
-2
2
5

Steps:

Run the algorithm to see the recorded steps.

No steps recorded yet.
SlowFast

Code

Insertion Sort is a sorting algorithm that builds the sorted portion of the list one element at a time. At each step it removes an element from the unsorted part of the array and inserts it in the correct position inside the already sorted part, shifting the other elements to the right.

Complexity: In the worst and average cases (reverse or random order), the time complexity is O(n²). In the best case (already sorted list), the algorithm only walks through the list once without needing to shift elements, resulting in O(n) time complexity. Like Bubble Sort, it is an in-place algorithm with O(1) space complexity.

1const insertionSort = (arr) => {
2    // Assume the first position is already sorted
3    for (let i = 1; i < arr.length; i++) {
4        let j = i;
5        
6        // Keep swapping with the left element while it is smaller
7        while (j > 0 && arr[j - 1] > arr[j]) {
8            [arr[j - 1], arr[j]] = [arr[j], arr[j - 1]];
9            j--;
10        }
11    }
12    return arr;
13};

Definition of Insertion Sort

Insertion Sort is a simple sorting algorithm that builds the sorted list progressively, one element at a time. It works very similarly to the way you would organize playing cards in your hands: you take a new card from the unsorted pile and insert it in the exact position among the cards that are already in order in your hand.

How Insertion Sort Works

  1. Assume that the first element of the list (index 0) is already in its final, sorted position.
  2. Take the next element
  3. Compare this element with the previous ones (which are already sorted), moving from right to left.
  4. While the previous element is greater than the current card, swap their positions to push the larger values to the right.
  5. The current card will find its correct spot. Repeat this process for all remaining items in the list.

Applications and Real-World Use

Unlike Bubble Sort, Insertion Sort is very useful in the real world. Although it is inefficient for huge and completely unsorted lists, it is incredibly fast for small lists (fewer than 50 items) or data that is already almost sorted. Because of this surgical efficiency, languages like Python and Java use hybrid algorithms (such as TimSort) that break huge lists into small chunks and trigger Insertion Sort under the hood to sort these fragments at lightning speed.

Properties of Insertion Sort

  1. Time complexity: The worst case (reversed list) and the average case have a time complexity of O(n²). However, its big advantage shines in the best case: when the list is already sorted or almost sorted, the complexity drops to O(n), since it makes only one comparison per element and almost no swaps.
  2. Space complexity: It is an in-place algorithm. It reorders the elements by moving them inside the original array itself, needing only one temporary variable for swaps. Therefore, its space complexity is O(1).
  3. Stability: Insertion Sort is a stable algorithm. If two elements have the same value, the algorithm will not swap their positions, perfectly preserving their original relative order.

FAQ

01. Is Insertion Sort better than Bubble Sort?

02. What does it mean to say that it is an 'adaptive' algorithm?

03. Why does it always start scanning from the second element (index 1)?

04. When should I choose to use Insertion Sort in my projects?

Decmatrix - Your open-source mathematical toolkit for calculations, conversions, and more.
Terms of Use
Privacy Policy
Contact: decimatrix25@gmail.com