public class selectionSort { public static int findPosBig (int[] sublist, int numItems) { //check if biggest by considering other elements int posBiggest = 0; for (int i=1; i< numItems; i++) { if(sublist[i] > sublist[posBiggest]) { posBiggest = i; } } return posBiggest; } public static void swapArrayItems (int[] list, int pos1, int pos2) { int temp = list[pos1]; list[pos1] = list[pos2]; list[pos2] = temp; } public static void main(String[] args) { int[] myList = { 10, 8, 6, 11, 7 }; // For i = n downto 2 for (int i = myList.length; i >= 2; --i) { //Find the first position of the largest item in the sublist //of the first i items from the list int posBig = findPosBig(myList, i); //Swap the largest item with the ith item; i.e. item at position i-1 swapArrayItems(myList, i-1, posBig); System.out.println(myList[0] + " " + myList[1] + " " + myList[2] + " " + myList[3] + " " + myList[4]); } } }