• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Implement the Insertion Sort Algorithm

laptop infographic online business 6087062

Introduction

Sorting algorithms are an essential part of computer science, and one of the simplest among them is the Insertion Sort. Though it’s not the fastest for large datasets, its ease of implementation make it a great starting point for understanding how sorting works.

What is Insertion Sort?

Insertion Sort works by gradually building a sorted section of an array. As it moves through the list, it picks one element at a time and places it where it belongs among the already sorted elements. Think of how you’d sort a hand of cards—you pick up one card, then place it in the correct position relative to the cards already in your hand.

This algorithm is in-place, meaning it doesn’t require extra memory, and stable, so equal elements retain their original order.

Insertion Sort Logic

The process involves:

  1. Starting from the second element of the array.
  2. Comparing it to the elements before it.
  3. Shifting those greater than it one position to the right.
  4. Inserting the current element into the proper location.
  5. Continuing this until the entire array is sorted.

When the array is already sorted, Insertion Sort runs efficiently with a time complexity of O(n). But in the worst-case scenario—like when the array is sorted in reverse—it slows down to O(n²).

Insertion Sort Java Implementation

Here’s a simple implementation of the algorithm in Java:

	@Test
	public void BubbleSortTest() throws Exception {
        int[] arrToBeSorted = {55, 66, 88, 99, 677, 55, 25, 77};
        bubbleSortingAlg(arrToBeSorted);
		for (int elem : arrToBeSorted) {
			System.out.println(elem + " ");
		}
	}

    private void bubbleSortingAlg(int[] arrToBeSorted) {
        int n = arrToBeSorted.length;
        boolean swappedElems;      
        for (int i = 0; i < n - 1; i++) {
            swappedElems = false;          
            for (int j = 0; j < n - i - 1; j++) {
                if (arrToBeSorted[j] > arrToBeSorted[j + 1]) {
	                int temp = arrToBeSorted[j];
	                arrToBeSorted[j] = arrToBeSorted[j + 1];
	                arrToBeSorted[j + 1] = temp;
	                swappedElems = true;
                }
            }
            if (!swappedElems) {
            	break;            	
            }
        }
    }

In this code, the insertionSort method loops through the array, moving each element into its appropriate place among the previously sorted values. The while loop handles the shifting of elements until the right spot is found for the current item.

Insertion Sort Use Cases

This method of sorting is ideal when dealing with small datasets or lists that are already almost sorted. It’s not efficient for large arrays due to its O(n²) time complexity in most cases, but it’s still commonly used in practice for short lists and as a building block in more complex algorithms.

Conclusion

Insertion Sort may not be the fastest, but it’s an important stepping stone for anyone learning about algorithms. Its logic is easy to follow, the code is short and readable, and it introduces core ideas like element comparison, in-place sorting, and algorithmic efficiency. is easy to follow, the code is short and readable, and it introduces core ideas like element comparison, in-place sorting, and algorithmic efficiency.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions