public class SelectionSort { public static void printArray(int[] a) { System.out.print("\nIndex i: "); for(int i=0; i< a.length; i++) System.out.printf("%2d, ", i); System.out.print("\nElement a[i]: "); for(int i=0; i< a.length; i++) System.out.printf("%2d, ", a[i]); System.out.println("\n\n"); } public static void main(String[] args) { int[] a= { 23, 78, 45, 8, 32, 56}; // Print the unsorted array printArray(a); // Sort the array using Selection Sort int minIndex; for(int i=0; i < a.length-1; i++) { // find the minimum element in the unsorted part of the array minIndex=i; for(int j=i+1; j < a.length; j++) if(a[j] < a[minIndex]) minIndex = j; // Swap a[i] with the smallest element int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } // Print the sorted array printArray(a); } }