Non Destructive selection sort on arrayList?

Status
Not open for further replies.
Hi, all.

Got a selection sort from a previous programming class for plain old arrays that
I've been trying to translate to array lists, but I'm missing key info on toArray
or an easy way to copy the fields from one to another.
Code:
int minIndex;
        int minItem;
        int start = 0;
        minIndex = start;

        for (int i = 0; i < array.length-1; i++)
        {
            //minIndex = findMinIndex(array, i);
            for (int index = start+1; index < array.length; index++)
        {
            if (array[index] < array[minIndex])
                minIndex = index;
        }

            //put the smaller item in place of the current item
            minItem = array[minIndex];
            array[minIndex] = array[i];
            array[i] = minItem;

            System.out.println("Array after pass " + (i+1) + " of selection sort:");
            for (int j = 0; j < array.length; j++)
                System.out.print(array[j] + "  ");
            System.out.println("");

        }//end Selection sort code

I've gotten this far playing around, and it seems to be comparing all, but the
way to copy to the tempArray and display doesn't work, or it's only storing
the first field because the swap/copy part isn't quite right.

Code:
public static void sortList (Scanner keyboard, ArrayList<Player> my_team)throws Exception
  {
        int minIndex;
        int minItem;
        int start = 0;
        minIndex = start;
      ArrayList<Player> tempArray = new ArrayList<Player>();
      //Comparable.class.
      //for (Player member : my_team)
      for (int i = 0; i < my_team.size()-1; i++)
      {
          //System.out.println(my_team.get(minIndex).getLastName());
          for (int index = start+1; index < my_team.size(); index++)
        {
            if (my_team.get(index).getLastName().compareTo(my_team.get(minIndex).getLastName())<0)
            {
                String compare_one = my_team.get(index).getLastName();
                String compare_two = my_team.get(minIndex).getLastName();
                System.out.println(compare_one + " is <  " + compare_two + " = "
                  + compare_one.compareToIgnoreCase(compare_two));
                minIndex = index;
            //System.out.println(my_team.get(index).getLastName());
            }
            //minItem = array[minIndex];
            //tempArray.set(i, my_team.get(minIndex));
            //System.out.println(my_team.get(minIndex));
            //array[minIndex] = array[i];
            tempArray.addAll(minIndex, my_team);
            //array[i] = minItem;
            

            //System.out.println("Array after pass " + (i+1) + " of selection sort:");
            //System.out.println(my_team.get(index).getLastName());
          
        }
          System.out.println(tempArray.get(i));

I've looked at this too much...I know I'm not seeing the obvious.
 

ZombieChipmunk

Ars Scholae Palatinae
1,001
Not sure if you are required to implement a selection sort. If not take a look at java.util.Collections.sort.

If you do need to implement, I would clean it up cosmetically by implementing Comparable interface in the Player class itself(using the lastname value) ;) Then you need to capture the value and swap them. I think this is what you are looking to do.

Code:
...

Player tmp = null;
for (int i = 0; i < my_team.size() - 1; i++)
{
   minIndex = i;   
   for (int j = i + 1; my_team.size(); j++)
   {
      if (my_team.get(minIndex).compareTo(my_team.get(j)) > 0) {
         minIndex = j;
      }
   }  
   if (minIndex != i) {
      tmp = my_team.get(i);
      my_team.set(i, my_team.get(minIndex));
      my_team.set(minIndex, tmp);
   }
}
...
 
Crap, the pain continues:

want a method in the super class "getInputFromConsole" to take in first and last name.
Thought it would be easy, but I can't call setFirstName or use it's variable (because
it is a separate method in the class...hummm...perhaps super.setFirstName?
Has to inherit from itself?

Much less @override the sub-classes' gIFC to append the stats (in this case the super was just Player, then Soccer was a given and and I've extended it, and added Quake and Baseball). Like Quake kills/deaths and baseball hits/strikes.

I know things would be a lot cleaner and simpler, but I can't figure for the life of me
how to move the methods in Main, to the super and subclasses.
 

ZombieChipmunk

Ars Scholae Palatinae
1,001
This is quick and ugly, but it seems in line with what you are trying to do. I am sure there are much better design patterns to use in the example (something that wouldn't need knowledge of what order the parsed values are in using a HashMap or whatever), but its monday morning and my brain isnt functioning yet.

Code:
class ConsoleInput {
...
   List<String> _inputs = new ArrayList<String>();

   List<String> getInputs() ...

   // parse the input from its format ... i.e. xml, whatever
   // for this example lets consider a values separated by ;
   void parseInputString(String input) { 
      for (String s : input.split(";") {
         _inputs.add(s);
      } 
   }
...
}

abstract class Player {
...

   String _lastName;
   String _firstname;

   void setLastName(String ln) {
      _lastName = ln;
   }
   void setFirstName(String fn) {
      _firstName = fn;
   }

   abstract void setIFC(ConsoleInput ci);
...
}

class QuakePlayer extends Player {
...
   void setIFC(ConsoleInput ci) {
       List<String> inputs = ci.getInputs();
       setLastName(inputs.get(0)); 
       setFirstName(inputs.get(1));
       ... grab the other Quake related values off the list
   }
...
}

main(..) {
...
   ConsoleInput qi = new ConsoleInput();
   qi.parseInputString("Doe;John;....");
   QuakePlayer qp = new QuakePlayer();
   qp.setIFC(qi);
...
}
 
Status
Not open for further replies.