JAVA Help Please

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
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


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:
So the code says whilst i >= 2, initially i = is the length of the vector,

list[0],..,list[3]
| 2 | 7 | 1 | 3 |

now the length of this vector is 4,
First iteration:
pos = 1 (the largest value of the first 4 values of list is at position 1)
swap element 4-1 for element 1 in list

list[0],..,list[3]
| 2 | 3 | 1 | 7 |

Next increment
i now = 3.
pos = 1.
swap element 3-1 for element 1 in list

list[0],..,list[3]
| 2 | 1 | 3 | 7 |

Next increment
i now = 2.
pos = 0.
swap element 2-1 for element 0 in list

list[0],..,list[3]
| 1 | 2 | 3 | 7 |

So what if you did an extra step?
i = 1
Now think about what findPositionOfBiggest() is doing.... find me the position of the largest value in the first 1 values of list... I think this just means its of order n-1.
 
Cheers for the reply.

Stuff like this will only annoy me if i don't underatand it :)

Think I underastand this now (had draw myself a diagram for each iteration lol :( ). I think the limit is set to >=2 to save an extra loop as its not needed as as when i=2 the list will look something like this (will bbe all sorted bar top 2 values):

Code:
Pos      array item:
0                   7
1            6
2                   8
3                 10
4                11
i-1 = 7
Biggest is 7 at index 0.

So i guess that limit is there to take the loop more efficient, as the loop could run the remaining times but wouldn't do anything. That's how i understand it.
 
Last edited:
Cheers for the reply.

Stuff like this will only annoy me if i don't underatand it :)

Think I underastand this now (had draw myself a diagram for each iteration lol :( ). I think the limit is set to >=2 to save an extra loop as its not needed as as when i=2 the list will look something like this (will bbe all sorted bar top 2 values):

Code:
Pos      array item:
0                   7
1            6
2                   8
3                 10
4                11
i-1 = 7
Biggest is 7 at index 0.

So i guess that limit is there to take the loop more efficient, as the loop could run the remaining times but wouldn't do anything. That's how i understand it.

Firstly don't think it's a bad thing to step through a a loop like this and draw diagrams, it's a good habit to get into making sure you properly understand what is going :)

Your right in that technically it would run for i>=1 but it'd be pointless and inefficient to do so. When i = 1, you have already successfully ordered the vector, why? Think about it more simply, lets pick out the largest values
You start with 10,8,6,11,7
First you pick, 11, then 10, then 8, then 7, by then you know the remaining thing (6) left is the smallest, you don't need to check it against the rest. It's the same for that java program at after the i=2 iteration, it has selected the n-1 largest values and ordered them, so it knows that the remaining value is the smallest.

Whilst your right that it could run for i >= 1 It could not however run for i>=0 to see why look at this line
swapArrayItems(list, i-1, pos);
if i was equal to zero that'd make i-1=-1 and hence you'd get a nasty "out of bounds" error. (there not a list[-1]).
 
Back
Top Bottom