Understanding Insertion Sort: A Simple and Efficient Sorting Algorithm

Yakub
4 min readSep 4, 2023

Sorting is a fundamental operation in computer science and plays a crucial role in various applications, from databases to search algorithms. One of the elementary and straightforward sorting algorithms is Insertion Sort. In this article, we will delve into the Insertion Sort algorithm, exploring its approach, time and space complexity, providing examples, and offering a Java code implementation for a comprehensive understanding.

Image Credit — Insertion-sort-0_1.png (902×808) (programiz.com)

What is Insertion Sort?

Insertion Sort is a simple comparison-based sorting algorithm that builds the final sorted array one item at a time. It is akin to the way we sort a deck of cards in our hands. Here’s a step-by-step explanation of the Insertion Sort algorithm:

  1. Initialization: As previously mentioned, Insertion Sort begins with the assumption that the first element of the array is already sorted. The algorithm splits the array into two parts: a sorted part and an unsorted part.
  2. Iterative Comparison: The algorithm iterates through the unsorted part of the array, one element at a time. For each element in the unsorted part, it compares it to the elements in the sorted part, moving from right to left. The goal is to find the correct position for the current element within the sorted part of the array.
  3. Insertion: Once the correct position is found, the algorithm inserts the element into the sorted part of the array. To do this, it shifts all the larger elements to the right by one position to make space for the current element. This shifting of elements continues until the correct position is reached, at which point the algorithm inserts the element.
  4. Repeat: Steps 2 and 3 are repeated until all elements from the unsorted part have been moved to the sorted part. This process continues until the entire array is sorted.

Java Code for Insertion Sort

Now, let’s dive into a Java code implementation of the Insertion Sort algorithm:

public class InsertionSort {
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1] that are greater than key
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6};
insertionSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}

This Java code implements the Insertion Sort algorithm. It takes an integer array as input, sorts it in-place, and prints the sorted array.

Approach Explanation

The Insertion Sort algorithm’s approach is relatively simple to grasp. It works like sorting a hand of cards: you take one card (element), compare it to the cards in your hand (sorted part of the array), and insert it in the correct position. This process continues until all cards are in order (the array is sorted).

The algorithm is efficient for small datasets but less so for large ones due to its quadratic time complexity.

Advantages of Insertion Sort

  • Simple to Implement: Insertion Sort is one of the simplest sorting algorithms to implement. It requires only a few lines of code, making it accessible for beginners and easy to understand.
  • In-Place Sorting: Insertion Sort sorts the array in-place, meaning it doesn’t require additional memory for storing temporary data structures. This makes it memory efficient.
  • Adaptive: Insertion Sort performs well on partially sorted arrays. If the data is already partially ordered, Insertion Sort can be quite efficient, as it only needs to make a few comparisons and shifts.
  • Stable Sorting: It is a stable sorting algorithm, which means that it maintains the relative order of equal elements in the sorted array.

Limitations of Insertion Sort

  • Quadratic Time Complexity: The major drawback of Insertion Sort is its time complexity. In the worst and average cases, it has a time complexity of O(n²), where ’n’ is the number of elements in the array. This makes it inefficient for large datasets.
  • Not Suitable for Large Data Sets: Due to its quadratic time complexity, Insertion Sort is not suitable for sorting large datasets. Other algorithms like Quick Sort or Merge Sort are more efficient for larger data sets.
  • No Parallelization: Insertion Sort does not lend itself well to parallelization, which means it cannot take full advantage of multi-core processors to speed up sorting.

Time and Space Complexity

Time Complexity: The time complexity of Insertion Sort is O(n²) in the worst and average cases, where ’n’ is the number of elements in the array. This is because, in the worst case, each element needs to be compared and possibly moved to its correct position within the sorted part of the array.

Space Complexity: Insertion Sort has a space complexity of O(1) because it sorts the array in-place, requiring only a constant amount of extra space for temporary variables.

Conclusion

Insertion Sort is a simple and intuitive sorting algorithm suitable for small datasets. It works by building the sorted array one element at a time, making it easy to understand and implement. However, its time complexity makes it inefficient for larger datasets compared to more advanced sorting algorithms like Quick Sort or Merge Sort. Understanding the Insertion Sort algorithm and its time and space complexity is valuable for anyone studying computer science or working on sorting problems.

--

--