Results 1 to 3 of 3
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Selection sort method?

    I'm trying to make a selection sort method that will sort a list of Strings alphabetically.
    I have a test list of Strings like so:
    Joe, Steve, Oscar, Drew, Evan, Brian, Ryan, Adam, Zachary, Jane, Doe, Susan, Fred, Billy-Bob, Cindy, Mildred, Joseph, Hammer, Hank, Dennis, Barbara

    However, whenever I run the method, the element that should go last, Zachary, in this case, ends up getting moved to the front for some reason. I'm not sure why.
    I tried changing what the first element was initialized to, to the variable i as that would logically work as well, but it ends up missing the first element in the list.
    Java Code:
    	public static void selectionStringAscendingSort (String[] words){
    		int i, j, first;
    		String temp; 
    		for ( i = 1; i < words.length; i++ )    {
    			first = 0; 						//initialize to subscript of first element
    			for(j = i; j < words.length; j ++){ 					//locate smallest element between positions 1 and i. 
    				if( words[ j ].compareTo(words[ first ]) <0 ) 
    					first = j;             
    			} 
    			temp = words[ first ]; //swap smallest found with element in position i.
    			words[ first ] = words[ i ];
    			words[ i ] = temp;     
    			System.out.println(Arrays.toString(words));
    		}
    		System.out.println(Arrays.toString(words));
    	}

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Selection sort method?

    Is this an assignment that you HAVE to do it yourself, or are you just looking to sort a set of words? Because of course the JDK provides ways to do it for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Selection sort method?

    Make your outer loop iterate from 0 (not 1!) to words.length and set first to i in the top of the body of your loop.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Selection sort to quick sort conversion without Nodes?
    By VettesRus in forum New To Java
    Replies: 0
    Last Post: 11-03-2013, 03:54 AM
  2. Selection Sort. please help!
    By cassato in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 11:26 PM
  3. Is this a Selection Sort?
    By Metastar in forum New To Java
    Replies: 2
    Last Post: 10-22-2010, 05:00 AM
  4. Problem with selection sort
    By Metastar in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 02:18 AM
  5. selection sort
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •