See More

import java.util.Scanner; public class InsertionSortSeparateMethod { // ---------- Insertion Sort Method ---------- public static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; // Shift larger elements to the right while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; // Insert key in correct position } } // ---------- Main Method ---------- public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter size of array: "); int n = sc.nextInt(); int[] arr = new int[n]; System.out.println("Enter " + n + " elements:"); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } // Calling the separate sorting method insertionSort(arr); System.out.println("Sorted Array:"); for (int num : arr) { System.out.print(num + " "); } sc.close(); } }