I'm trying to do selection sort for objects...But it wont work, please help. We were told to change the first one to do objects. Any help appreciated

public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   public static void selectionSort (Comparable[] objects)
   {
      int min;
      	Comparable temp;

      for (int index = 0; index < objects.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < objects.length; scan++)
            if (objects[min].compareTo(objects[scan]))
               min = scan;

         // Swap the values
         temp = objects[min];
         objects[min] = objects[index];
         objects[index] = temp;
      }
   }

for starters:
what are you testing for here:

if (objects[min].compareTo(objects[scan]))

also, you're shifting the value of 'min' quite some times. didn't really fully read the code, but are you sure that doesn't cause any problems?

I think, though I am not 100% sure, that the problem is rooted in the if (objects[min].compareTo(objects[scan])) statement over here. compareTo() returns either 1,0,-1 depending on whether the calling object is greater than, equal to or less than the object passed as a parameter.
According to the above statement you seem to be of the notion here that if(1) would make the control move into the if condition. But I don't think if(1) would be good enough, according to The Java Language Specification the if() structure requires the expression to result in a boolean type, else it gives a compile-time error. Is your program compiling correctly? even if it is I suggest you get the if(1) structure confirmed.

To elaborate on my previous post further, if the result of the expression in the if stucture is of the type Boolean it is converted to boolean by using the method booleanValue of the Boolean class. These conversions are called Unboxing Conversions You can read more about them here

Having said all this, if the point mentioned by me is not what is causing the problem in your case then I suggest, you let us know (and know yourself, very clearly) what is that that you are comparing. There are certain things that you as the implementor need to be careful while using compareTo . These things are mentioned in the javadocs for compareTo

PLease help, here are the files. They already gave the object one with insertion sort, but I can't do selection

Attachments
//********************************************************************
//  SortPhoneList.java       Author: Lewis/Loftus/Cocking
//
//  Driver for testing an object sort.
//********************************************************************

public class SortPhoneList
{
   //-----------------------------------------------------------------
   //  Creates an array of Contact objects, sorts them, then prints
   //  them.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Contact[] friends = new Contact[7];

      friends[0] = new Contact ("Barika", "Favaken", "610-555-7384");
      friends[1] = new Contact ("Lorenz", "Lowski", "215-555-3827");
      friends[2] = new Contact ("Ely", "Kassakian", "733-555-2969");
      friends[3] = new Contact ("Galen", "Powers", "663-555-3984");
      friends[4] = new Contact ("Laura", "Getz", "464-555-3489");
      friends[5] = new Contact ("Ching", "Lee", "322-555-2284");
      friends[6] = new Contact ("Susan", "Getz", "243-555-2837");

      Sorts.insertionSort(friends);

      for (Contact contact: friends)
         System.out.println (contact);
   }
}
//********************************************************************
//  Sorts.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the selection sort and insertion sort algorithms,
//  as well as a generic object sort.
//********************************************************************

public class Sorts
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   public static void selectionSort (Comparable[] objects)
   {
      int min;
      	Comparable temp;

      for (int index = 0; index < objects.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < objects.length; scan++)
            if (objects[min].compareTo(objects[scan]))
               min = scan;

         // Swap the values
         temp = objects[min];
         objects[min] = objects[index];
         objects[index] = temp;
      }
   }
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers)
   {
      for (int index = 1; index < numbers.length; index++)
      {
         int key = numbers[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key)
         {
            numbers[position] = numbers[position-1];
            position--;
         }
            
         numbers[position] = key;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects)
   {
      for (int index = 1; index < objects.length; index++)
      {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0)
         {
            objects[position] = objects[position-1];
            position--;
         }
            
         objects[position] = key;
      }
   }
}

Did you happen to read the above posts ? Did you try out the things mentioned there ? Did you go through the links ? Those may help you in eradicating your problem. Have you really 'tried' to help yourself ?

This article has been dead for over six months. Start a new discussion instead.