Having trouble with selection sort...

I'm trying to get the numbers to sort in ascending order but I'm having some trouble...

import java.util.ArrayList;

publicclass OneDArrayList

{

publicstaticvoid main (String [] args)

{

int i2=0;

int nums[] =newint[10];

ArrayList<Integer> numbers =new ArrayList<Integer>();

while(i2 < 10)

{

numbers.add(i2, (int)(Math.random()*10));

i2++;

}

int sum = sumVal (numbers);

numbers = addVal (numbers);

int small = smallestVal (numbers);

numbers = sort (numbers);//this may be where I'm going wrong?

System.out.println("Sum is " + sum);

System.out.println("Adding one to the array is ");

for(int i = 0; i < numbers.size(); i++)

{

System.out.print(" " + numbers.get(i) +" ");

}

System.out.println();

System.out.println("Smallest number is " + small);

int last = findValue(numbers);

System.out.println("First value is " + last);

}

publicstaticint sumVal (ArrayList<Integer> myA)

{

int equals = 0;

for (int i=0; i < 10; i++)

{

equals = equals + myA.get(i);

}

return equals;

}

publicstatic ArrayList<Integer> addVal (ArrayList<Integer> myA)

{

for (int i=0; i < myA.size(); i++)

{

myA.set(i, myA.get(i) + 1);

}

return myA;

}

publicstaticint smallestVal (ArrayList<Integer> myA)

{

int x = 10;

for (int i=0; i < myA.size(); i++)

{

if (myA.get(i) < x)

{

x = myA.get(i);

}

}

return x;

}

publicstaticint findValue (ArrayList<Integer> myA)

{

int counter = 3;

int i = 0;

for (i=0; i<myA.size(); i++)

{

if (myA.get(i) == counter)

{

return i;

}

}

return i;

}

publicstatic ArrayList><Integer> sort (ArrayList<Integer> myA)

{

int i, iMax, n, aTemp;

for(n = 10; n>=2; n--)

{

iMax = 0;

for (i=1; i<n; i++)

{

if(myA.get(i) > myA.get(iMax))

{

iMax = i;

}

}

aTemp = myA.get(iMax);

myA.set(iMax, myA.get(n-1));

myA.set(myA.get(n-1), aTemp);

}

return myA;

}

}

Any hints would be awesome.

[5315 byte] By [SPortnova] at [2007-11-26 15:53:39]
# 1
Could we get some hints on what is happening now? Is it sorting wrong? Not compiling? if it is sorting wrong can you give us some example input and output please? One can better spot an issue in sorting type code if one knows what the code is doing now.
cotton.ma at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 2

It's not that it's sorting wrong it's that I'm not exactly sure where to call the method to get it to work accordingly.

In one instance it would sort half of the arraylist but the other half would remain unsorted...

It's relatively difficult to explain due to my own lack of expertise in Java programming.

The sort, to me, looks correct - I'm just not sure of how to implement it within my code to make everything hunky dorey.

SPortnova at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 3
for(n = 10; n>=2; n--)My guess it is not sorting the first two elements in the list.
floundera at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 4

That was my hunch as well; however, changing it to zero led to the obvious outofbounds error being thrown and changing it to one had much the same effect as what happened when it was two...

It would...sort of sort, let me explain...

4 4 7 4 7 4 7 9 9 7

It keeps on doing something that extent and I can't seem to figure out why...

SPortnova at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 5

I just ran a modified version of your code. Printed out the list before soirting and after sorting.

8 3 9 4 7 1 3 0 7 5

3 3 5 3 7 5 3 7 7 9

As you can see not all the values in the original list are present in the final list. Looks like when you are doing the swap you are not copying the values into the correct places in the list.

floundera at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 6
What is going on here?aTemp = myA.get(iMax);myA.set(iMax, myA.get(n-1));myA.set(myA.get(n-1), aTemp);it looks suspicious...
cotton.ma at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 7
myA.set(myA.get(n-1), aTemp); // bogus line
floundera at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 8
I figured it out...aTemp = myA.get(iMax);myA.set(iMax, myA.get(n-1));myA.set(myA.get(n-1), aTemp);had to be changed to...aTemp = myA.get(iMax);myA.set(iMax, myA.get(n-1));myA.set((n-1), aTemp);Thanks guys!
SPortnova at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...
# 9
Sweet!
floundera at 2007-7-8 22:14:09 > top of Java-index,Java Essentials,New To Java...