public class Demo { public static void selectionSort(String[] list) { for (int i = 0; i < list.length-1; i++) { /* -------------------------------------------------- (1) Find the minimum in the list[i..list.length-1] -------------------------------------------------- */ String min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].compareTo(min) < 0 ) // Found a smaller element { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ (2) Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] String help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } }