Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

selection sort on linklist

 
Anuja Navare
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other way to do the selection sort?
I am trying to use selection sort on a linklist to sort Double numbers. I wrote a method to find the smallest number as follows:

Then I used the thePosition which is a LinkList to swap the first value with the smallest. For that I wrote another method as follows:

I thought since in for loop it says p = p.itsNext, it will find the smallest value in the emaining list afte it swapped the smallest value with the 1st value in the list. Somehow its is not sorting properly. What am i doing wrong? Or is there any other way to do the selection sort? Removing the largest number & putting it, doesn't work as it inserts the number after the 1st value - a friend has already tried that. Please help!!!
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited May 14, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you left out some stuff.
itsNext //this is not a method, so is it a field?
itsData //ditto
isEmpty() //where is this
thePosition // not defined locally???

Why would you set p=r and then in the for loop reset it to "this" instead of what was explicitely passed in?
Is this compiling at all???
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is posted in Intermediate, so I am closing it here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic