DEV Community

Shreehari Menon
Shreehari Menon

Posted on • Originally published at Medium on

Insertion Sort

“Efficiency in Simplicity”


Photo by Scott Graham on Unsplash

Introduction

When one starts learning the basics of programming be it any language,

I 'am sure you would have encountered searching and sorting algorithms, as they form the foundation of computer science and programming.

Let’s keep searching algorithms for another article, here let’s dive into sorting algorithms and understand them, in particular, let’s discuss about insertion sort.

Sorting algorithms have been a cornerstone of computer science since its early days. In the 1940s, as computers were being developed, sorting became crucial for processing large amounts of data efficiently. Early methods like bubble sort and insertion sort were simple yet effective for small datasets.

Sorting algorithms are taught to beginners as they provide a fundamental understanding of how to organize and manage data efficiently. By learning sorting, one can grasp key programming concepts like loops, conditional statements & arrays, while also developing problem-solving skills.

Sorting algorithms help organize data, making it easier to find and use. For example, sorting a list of names alphabetically makes it much quicker to search for someone.

Why Sorting Algorithms Matter

Sorting helps students realize the importance of optimizing solutions, as they compare simple algorithms like bubble sort with more advanced ones like quicksort, which perform better on large datasets.

Sorting algorithms are essential for real-world applications such as organizing files, searching data, and processing information in databases and search engines. These algorithms also lay the groundwork for more complex topics in computer science, making them an important starting point for further learning.

There are numerous sorting algorithms available, but typically, students are taught only a few basic ones, while the others are overlooked.

Let me list out some popular ones: Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort.

I ‘am sure you would have heard of the first two,

Bubble Sort : Repeatedly compares and swaps adjacent elements until the list is sorted, with larger elements "bubbling" to the end.

Selection Sort : Select the smallest (or largest) element from the unsorted part of the list and swap it with the first unsorted element.

What is Insertion Sort?

Now let’s discuss about insertion sort in detail. Insertion sort is a simple yet efficient algorithm that’s actually very simple to understand.

Insertion sort is a straightforward sorting algorithm that produces the final sorted list one element at a time. It operates by selecting elements from the unsorted list and inserting them into the appropriate location in the sorted list. This is accomplished by comparing each element to the ones that came before it and rearranging them as necessary to make room for the new ones.

How Does Insertion Sort Work?

Imagine a man holding a set of cards, typically we use our left hands to arrange and handle the cards, while our right hands are employed to arrange, choose, or insert a card. We all know that cards have a specific order starting with ace, king, queen, jack, 10, 9, ……2.

Assume that the left hand is holding the sorted section, and the right hand is picking up a card from the unsorted section and inserting it into the appropriate location among the sorted cards.

Now let’s understand this process step by step :

Initially, there is just one card in his left hand, it’s considered sorted.

Then we choose a card from the unsorted lot and place it in its appropriate position, i.e. place it on the right side of the first card if its value is less else to the left.

Similarly, now the next card is placed either in between the two cards on either ends depending on its value. This step is repeated until we have sorted the entire pack.

Time and Space Complexities :

  • Time Complexity : Best case : O(n) — when the list is already sorted. Average and Worst case : O(n²) — when the list is unordered or nearly sorted.
  • Space Complexity : O(1) — Insertion sort is an in-place sorting algorithm, meaning it doesn’t require extra space for sorting, except for a small amount of space used for temporary variables.

Pros and Cons :

Pros :

Simple to Implement : Insertion sort is straightforward to understand and implement, making it a good algorithm for beginners.

Efficient for Small Data Sets : For small lists or nearly sorted data, insertion sort is very efficient since it can perform with O(n) time complexity in the best case.

Stable : It doesn’t change the relative order of equal elements, which can be important in certain applications.

In-Place Sorting : It doesn’t require extra memory beyond the input list, making it memory efficient.

Cons :

Inefficient for Large Data Sets : The O(n²) time complexity in the average and worst case makes it impractical for large lists.

Slower than More Advanced Algorithms : When compared to algorithms like quicksort or merge sort, insertion sort is much slower for larger datasets, as it involves more comparisons and shifts.

*Implementation * :

import java.util.*;
public class InsertionSort {
public static void insertionSort(int[] arr) {
int len = arr.length;
/* start from 1 and not 0 as initially it's 
 * considered sorted with just 1 item */
for (int i = 1; i < len; i++) {
int key = arr[i];
// pointer to index just before the key's index
int j = i - 1;
/* move values in arr[0...i-1] greater
 * than key one position to their right */
while (j >= 0 && key < arr[j]) {
arr[j + 1] = arr[j];
j--;      
}
/* store the key just before the last shifted element
 * or after the element just smaller than or equal to the key */
arr[j + 1] = key;    
}
}
public static void main(String[] args) {
int[] arr = { 15, 11, 7, 10, 9 };
insertionSort(arr);
System.out.println(Arrays.toString(arr));
}
}
Enter fullscreen mode Exit fullscreen mode

Real Life examples

Insertion sort, while simple, is extremely useful in some situations:

Tiny lists: it’s good for sorting tiny lists because of its low overhead and simple implementation.

Nearly Sorted Data: Insertion sort works best when data is nearly sorted since it requires fewer comparisons and shifts.

Real-Time Sorting: In circumstances where data enters incrementally (for example, stock price changes), insertion sort can sort it live.

Embedded systems: Insertion sort is a good choice for situations with limited memory or computing capacity, such as microcontrollers, because of its in-place nature and simplicity.

By understanding these use cases, it’s clear that insertion sort, though basic, has practical value in solving specific problems efficiently.

Conclusion

Insertion sort may not be the most efficient algorithm for large datasets, but its simplicity, stability, and effectiveness on small or nearly sorted data make it a fundamental part of understanding sorting algorithms. As one of the first sorting techniques taught to beginners, it provides a stepping stone to more advanced concepts in algorithm design and optimization.

By learning insertion sort, you not only gain insights into how sorting works but also develop problem-solving skills that are essential for tackling more complex challenges in computer science. Whether you’re sorting a deck of cards or analyzing large datasets, the principles behind insertion sort remind us of the importance of efficiency and precision in organizing data.

References

Levitin, A. (2012). Introduction to the design & analysis of algorithms (pp. vii–x). Pearson Education, Inc.

RichardsSoftware.net — CLRS Algorithm a Day: Insertion Sort. (n.d.). http://richardssoftware.net/Home/Post/76

Juneja, J. (2022, October 19). Insertion Sort in Java with Examples and Use Cases — Scaler Topics. Scaler Topics. https://www.scaler.com/topics/insertion-sort-in-java/

Card game Stock Photos, Royalty Free Card game Images | Depositphotos. (n.d.). Depositphotos. https://depositphotos.com/photos/card-game.html?qview=358081532

Top comments (0)