Skip to main content

Insertion Sort Example in Java

3 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Insertion Sort is a classic Java console program that demonstrates the concept with complete source code and sample output. Sorting algorithms arrange data in order — bubble, selection, merge, quick and heap sort.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages like simple implementation, efficient for (quite) small data sets, more efficient in practice than most other simple quadratic algorithms, adaptive, stable, in-place; i.e., only requires a constant amount of additional memory space, online; i.e., can sort a list as it receives it

Insertion Sort Example Program

import java.util.Arrays;
 
public class InsertionSort {
    public static void main(String[] args) {
        int arr1[] = new int[10];
        populateArray(arr1);
        System.out.println("Numbers to be sorted: ");
        printArray(arr1);
        insertionSort(arr1);
        System.out.println("\nAfter Sorting: ");
        printArray(arr1);
    }
    private static void insertionSort(int[] arr2) {
        for (int i = 1; i < arr2.length; i++) {
            int sort = arr2[i];
            int j = i;
            while (j > 0 && arr2[j - 1] > sort) {
                arr2[j] = arr2[j - 1];
                j--;
            }
            arr2[j] = sort;
        }
    }
    public static void printArray(int[] arr3) {
        System.out.println(Arrays.toString(arr3));
    }
    public static void populateArray(int[] arr3) {
        for (int i = 0; i < arr3.length; i++) {
            arr3[i] = (int) (Math.random() * 100);
        }
    }
}

Sample Output

Numbers to be sorted:
[29, 6, 25, 7, 15, 86, 48, 78, 29, 4]

After Sorting:
[4, 6, 7, 15, 25, 29, 29, 48, 78, 86]

When to use

Use this insertion sort example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Arrays; imports a class used later in the program.

  3. int arr1[] = new int[10]; updates a variable used in the calculation or output.

  4. A println / print call writes text to the console — part of the sample output below.

  5. A println / print call writes text to the console — part of the sample output below.

  6. for (int i = 1; i < arr2.length; i++) { updates a variable used in the calculation or output.

  7. int sort = arr2[i]; updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Insertion Sort program demonstrate?
It shows how to implement insertion sort in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials