Start Coding Now
⚙️ Algorithm Programsintermediate1 methods

Insertion Sort in Java

Java Program to implement Insertion Sort algorithm.

Last updated: 11 January 2026

Method 1: Standard Implementation

Sorting by building sorted array.

public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {9, 5, 1, 4, 3};
        
        for (int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
        
        for (int num : arr) System.out.print(num + " ");
    }
}
Output:
1 3 4 5 9

Explanation

Pick element and place it in correct position in sorted part.

Frequently Asked Questions

Try This Program

Copy this code and run it in our free online Java compiler.

Open Java Compiler