0

Im working on a program that alphabetically sorts a string array using compareTo method and selection sort.


Im having an issue within my minimumPosition method below. The method is designed to take the smallest element in a tail region of the array so that the selection sort program can conveniently sort the list.

My issue is that when I sort the list and print it via the tester it prints it out reverse alphabetically with a decrepency in-front. e.g. (c ,z,x,y...,b,a) opposed to (a,b,c.. y,x,z)

/**
   SelectionSorter class sorts an array of Strings alphabetically.
   It uses the selection sort algorithm.

 */
public class SelectionSorter
{
    private String[] a;
    /**
       Constructs the selection sorter
       @param anArray the array to sort
     */
    public SelectionSorter4 (String[] anArray)
    {
        a = anArray;
    }


    /**
       Sorts the array managed by this selection sorter
     */
    public void sort ()
    {
        for (int i = 0 ; i < a.length - 1 ; i++)
        {
            int minPos = minimumPosition (i);
            swap (minPos, i);
        }
    }


    /**
       Finds the smallest element in a tail region of the array.
       The elements are String objects in this case, and the
       comparison is based on the compareTo method of String.
       @param from the first position of the tail region
       @return the position of the smallest element in tail region
     */
    private int minimumPosition (int from)
{

    String holder = a [from];
    int position = from;
    for (int i = from ; i < a.length ; i++)
    {
        if (a [i].compareTo (holder) > 0)
        {
            holder = a [i];
            position = i;
        }

    }
    return position;                
}

        /**
           Swaps two entries of the array
           @param i the first position to swap
           @param j the second position to swap
         */
        private void swap (int i, int j)
        {

            String temp = a [i];
            a [i] = a [j];
            a [j] = temp;

        }
    }

Tester class: Relevant but there are no issues here.

    /**
       Tests the SelectionSorter4 class which sorts an array of Strings
       alphabetically.
     */
    import java.util.* ;

    public class SelectionSorterTester
    {
        public static void main(String[] args)
        {
            String[] a = randomStringArray(40, 6) ; 
            SelectionSorter sorter = new SelectionSorter(a) ;

            System.out.println(toString(a)) ;
            sorter.sort() ;
            System.out.println("----------Sorted:") ;
            System.out.println(toString(a)) ;
            System.out.println("--------------------------------") ;
        }
        /**
           Returns a string representation of the array of Strings
           @param array the array to make a string from
           @return a string like [a1, a2, ..., a_n]
         */
        public static String toString(String[] array)
        {
            String result = "[" ;
            for (int i = 0 ; i < array.length - 1; i++) {
                result += array[i] + ", " ;
            }
            result += array[array.length - 1] + "]" ;
            return result ;
        }
        /**
           Creates an array filled with random Strings.
           @param length the length of the array
           @param n the number of possible letters in a string
           @return an array filled with length random values
         */
        public static String[] randomStringArray(int length, int n)
        {
            final int LETTERS = 26 ;
            String[] a = new String[length] ;
            Random random = new Random(53) ;
            for (int i = 0 ; i < length ; i++) {
                String temp = "" ;
                int wordLength = 1 + random.nextInt(n) ;
                for (int j = 0 ; j < wordLength ; j++) {
                    char ch = (char)('a' + random.nextInt(LETTERS)) ;
                    temp += ch ;
                }
                a[i] = temp ;
            }
            return a ;
        }
    }

I think the issue lies within the minimumPosition method but it looks correct to me.

3
  • Please have a look at how to debug java code Commented Dec 13, 2013 at 2:17
  • On first glance, shouldn't position maybe be set to from rather than 0? Commented Dec 13, 2013 at 2:19
  • My bad. Thanks splrs. I still have the issue though that the array is printed reverse alphabetically. And I guess debugging would be a good thing Commented Dec 13, 2013 at 2:20

1 Answer 1

3

If you want ascendant order,

Change

 if (a [i].compareTo (holder) > 0)

to

 if (a [i].compareTo (holder) < 0)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Read more: Comparable#compareTo(..)

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't this now an unstable sort?
@ElliottFrisch unstable means not stable?AFAIK selection sort is not stable
It can be, if the minimum value is inserted into the first position (instead of swapping) - say using a LinkedList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.