public class InsertionSort { public static void main(String[] args) { int[] array = {5, 2, 8, 12, 1, 6, 4}; System.out.println("Unsorted array: " + Arrays.toString(array)); insertionSort(array); System.out.println("Sorted array: " + Arrays.toString(array)); } public static void insertionSort(int[] array) { int n = array.length; for (int i = 1; i < n; i++) { int current = array[i]; int j = i - 1; // Move elements greater than current to the right while (j >= 0 && array[j] > current) { array[j + 1] = array[j]; j--; } // Insert the current element at the correct position array[j + 1] = current; } } }