• Post Reply Bookmark Topic Watch Topic
  • New Topic

selection sort of an array list string  RSS feed

 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a school project that requires 10 names added to an arraylist that is then sorted by selection sort alphabetically and in reverse.  So I have a working program but I had to use collections.sort to do what I needed.  (You can see my code below).  It works fine.  However it isn't a selection sort and after two days of searching on the web I have yet to find any good examples of using selection sort to sort an array list of strings.  Any hints you guys can provide would be greatly appreciated. All of the examples I've seen are sorting integers not strings.

Thanks!

 
Paul Clapham
Sheriff
Posts: 22839
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Houser wrote:Any hints you guys can provide would be greatly appreciated. All of the examples I've seen are sorting integers not strings.


Hint: Sorting integers is not different from sorting strings. Sure, for integers you say "less than, greater than" and for strings you say "before, after" but that's just a triviality. What is it which is holding you up in the strings-versus-integers issue?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort to sort object references is a "modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist).

Try googling: selection sort algorithm java
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Sorting integers is not different from sorting strings. Sure, for integers you say "less than, greater than" and for strings you say "before, after" but that's just a triviality. What is it which is holding you up in the strings-versus-integers issue?

You know, I hadn't thought of it that way, I was hung up on only seeing integers in the examples.  This helps, thanks.

The algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort to sort object references is a "modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist).

Try googling: selection sort algorithm java


I hadn't read up on the Arrays.sort, Iwill have to do that, and I am going to go do some google searching now. 

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For comparing Strings check out the String compareTo() method.
 
Paul Clapham
Sheriff
Posts: 22839
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Houser wrote:You know, I hadn't thought of it that way, I was hung up on only seeing integers in the examples.  This helps, thanks.


Glad to hear it. I've been programming so long that I don't always identify with problems which beginning programmers encounter, so I'm pleased to have been of help this time.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also a field in the String class which supplies a ready‑made Comparator object which does alphabetical comparisons in a case insensitive fashion. If you have a Comparator, there are methods in Comparator which will create another equivalent to that Comparator backwards. You can use similar techniques as you used for numbers, but comparing the results of the compare or compareTo method with 0.
I would suggest you create a utility method which swaps two elements in your List; you will probably use the get() and set() methods to move elements. Beware: if you try swapping elements in a linked list like that, you are liable to get slow execution, but there shou‍ld be no speed problems for an array list.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!