Hi,
I'm going through a few Java tutorials to get myself back uop to speed and for the life of me can't work this out - its a basic tutorial about sorting arrays using loops (via not importing the java.util.arrays). The code is below (see highlighted part), I can't work out why they loop for i>=2, i would have thought it would be i>=0. Have been staring at it for a while now aswell.
Thanks
I'm going through a few Java tutorials to get myself back uop to speed and for the life of me can't work this out - its a basic tutorial about sorting arrays using loops (via not importing the java.util.arrays). The code is below (see highlighted part), I can't work out why they loop for i>=2, i would have thought it would be i>=0. Have been staring at it for a while now aswell.
Thanks
Code:
public class SelectionSort {
public static void main(String[] args) {
//Create an array to test the selection sort out on
int list[] = {10,8,6,11,7};
//Store the index of the biggest item in the array
int pos;
//Output the unsorted array
System.out.println("Original list");
printArray(list);
//Loop through the array from the bottom upwards
for (int i = list.length;[COLOR=Red][B] i>=2[/B][/COLOR]; i--){
//Find the position of the largest item of i items in array list
pos = findPositionOfBiggest(list,i);
//Swap the largest item (at position pos) with the item at position i-1
swapArrayItems(list, i-1, pos);
}//end for
System.out.println("\nNew list");
printArray(list);
System.exit(0);
}//end main
Last edited: