• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bubble sorting

 
Jamie Cotton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya i have an array of objects called messageArray which is created by converting an ArrayList to an Array



The array has objects in it each called message and a message is made up of
Now i want to sort the new Array which i have created by the sender by using bubble sort but not sure how can any help ?

At the moe the print out is

but after the sort i want

[ November 20, 2004: Message edited by: Jamie Cotton ]
[ November 20, 2004: Message edited by: Jamie Cotton ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do it like that?
And why care about the algorithm at all?

The easiest way would be to make your class Comparable and pipe the List into a SortedSet. It will then be sorted automagically.
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JDSL has some specific sorts (e.g. quicksort, mergesort), but not bubble sort. see if any of those give you good enough performance
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason you want to use bubble sort? I can understand if this is a homework assignment and the teacher has specified that you do so. However, if not, you should use Collections.sort() on the original ArrayList instead. In fact, why are you converting the ArrayList into an array? Also, if you want the list sorted, there are other collections that are better to use. TreeSet will put the items in a sorted order as you add them. You can also use an Iterator to iterate over the set (or even the original ArrayList), rather than converting it into an array.

HTH

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic