import java.util.Scanner; public class SelectionSortUserInput { // ---------- Selection Sort Method ---------- public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; // Find the index of the minimum element for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // Swap int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } // ---------- 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(); } // Call the selection sort method selectionSort(arr); System.out.println("Sorted Array:"); for (int num : arr) { System.out.print(num + " "); } sc.close(); } }