File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes selection sort on linklist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "selection sort on linklist " Watch "selection sort on linklist " New topic

selection sort on linklist

Anuja Navare

Joined: May 13, 2001
Posts: 6
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"

Joined: Sep 29, 2000
Posts: 8521
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???

"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
This is posted in Intermediate, so I am closing it here.
I agree. Here's the link:
subject: selection sort on linklist