compareTo() problems

From: TookieClothespin (krista2882_at_gmail.com)
Date: 02/24/05


Date: Thu, 24 Feb 2005 10:21:34 -0500

I'm trying to sort an array of strings using Selection Sort. Im using
CompareTo to compare the string elements in the array to each other, but
the loop where I use this method is being skipped over.
Does anyone know an alternate way to do this so that the program actually
enters the loop?
Thaks for your help!

here's my code:

      public void selectionSort(String[] array)
      {
         int compcount = 0;
         int excount = 0;
         String temp;
         for (int i=0; i<array.length-1; i++)
         {
            for (int j= i+1; j < array.length; j++)
            {
               if (array[i].compareTo(array[j]) > 0)
               {
                  // exchange elements
                  temp = array[i];
                  array[i] = array[j];
                  array[j] = temp;
                  excount++;
              }
              compcount++;
           }
         }
         System.out.println("Selection Sort: " + excount + " exchanges,
and " + compcount + " comparisons.");
      }



Relevant Pages

  • Re: compareTo() problems
    ... > I'm trying to sort an array of strings using Selection Sort. ... > CompareTo to compare the string elements in the array to each other, ...
    (comp.lang.java.help)
  • Re: Sorting
    ... Then go back to the start of the array and do it again, until you make a clean sweep without swapping. ... This algorithm is called bubblesort. ... However I am not saying that you are wrong that, overall, selection sort might not be a better introductory algorithm than bubblesort. ...
    (comp.lang.c)
  • Re: is there a way to speed up this tablesort code?
    ... > It is error-prone to add new properties to host objects like ... > Selection sort is probably the most intuitive sorting algorithm, ... > array by criteria using Array.prototype.sort ... If the first parameter of Node::insertBefore ...
    (comp.lang.javascript)
  • Re: SOME DIFFERENT KIND OF SELECTION SORTING OF ARRAY.
    ... SELECTION SORT IS BASED ON D FOLLOWING IDEA: ... SELECTING D LARGEST ARRAY ELEMENT AND SWAPPING IT WITH THE LAST ARRAY ... The programming community here ...
    (comp.lang.c)
  • Re: recursive functions
    ... sub multiply_em { ... find the minumum value in an array of ints ... sort an array using the strategy of the selection sort ... search through a sorted arrya using the strategy of the binary search ...
    (comp.lang.perl.misc)