This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is wrong with my generic implementation of sort method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is wrong with my generic implementation of sort method ?" Watch "What is wrong with my generic implementation of sort method ?" New topic
Author

What is wrong with my generic implementation of sort method ?

Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
Hi
For Input: [abc, abb, aaa, bbc]

Collections.sort give: [aaa, abb, abc, bbc]

I tried to implement something similar to it like this



But what i get is: [abb, aaa, abc, bbc]


Why ?.....Any Help !!


Praveen
SCJP, SCWCD, SOA
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It's probably just a bad implementation of bubble-sort. Nothing to do with generics, I wouldn't think.

(It would be better if the method's name was "doBubbleSort" rather than "doBubbleShort" but that isn't the source of your problem.)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Think about the scope of your inner loop.

Because you're looping from the start to finish, at the end of one iteration you've got the last element in the right place. So the next iteration you need to move across all elements except the last one. But instead, you're moving across all but the first.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you change your method definition to:
Then you don't need to cast. Also oneSwap is never used.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Wouter Oet wrote:If you change your method definition to:
Then you don't need to cast. Also oneSwap is never used.

Make that Without the ? super part you couldn't sort a List<java.sql.Timestamp>, as Timestamp extends java.util.Date and therefore implements Comparable<Date> - not Comparable<Timestamp>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
Thanks Paul and Matthew
Its end up in wrong implementation only !
I almost presume that error is in 'generic' logic only
Final code



Thanks Wouter
Its works very well
I definitely need more study on Generics !

Thanks Rob
Look like you are an expert on generics

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Expert, no. I am somewhat experienced though, even though I didn't switch from Java 1.4 to Java 5.0 for the first year or so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is wrong with my generic implementation of sort method ?
 
Similar Threads
u_int8_t in java
complicate JSP tag : bodyContent
am i missing something?
how to create 5 objects of a class
Can you help me plzzz with a graph?