Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Java Programming Question, selectionSort doubleylinkedlist
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
gbkyle
Apprentice
Apprentice


Joined: 29 Nov 2004
Posts: 150
Location: Jersey

PostPosted: Mon Nov 21, 2005 11:59 pm    Post subject: Java Programming Question, selectionSort doubleylinkedlist Reply with quote

Hey im having some trouble with getting a selection sort to work in a doublylinked list, my selection sort code reads:

Code:

 public void selectionSort ()
        {
                int counter=0;
                LinearNode<T> temp = null;
                LinearNode<T> previous;
                LinearNode<T> min;                 LinearNode<T> data;
                data=contents;

                for(int index=0; index<count-1;index++)
                {
                        min=data.getNext();
                                for(int scan=index+1; scan<count;scan++)
                                {
                                        if ((data.getElement()).compareTo(min.getElement())<0)
                                                {
                                                        min=data;
                                                }                      previous=data;
                        data=data.getNext();
                        }
                counter++;

                //Swap the values
                T firstob=(remove(min.getElement()));
                if(data.getElement().compareTo(firstob)!=0 || counter<=14)
                {
                        add(data.getElement());
                }

                System.out.println(contents.getElement()+"SORT");
                System.out.println(firstob+"SORT");
                contents.setElement(firstob);
                data=contents;
                        for(int x=0; x<=counter-1;x++)

                   {
                        data=data.getNext();
                        }
                }
                remove(data.getElement());
                contents.setElement(data.getElement());         }


I can have my program output my lit fine... its just whenever i run the sort, 1 element dissapears, and i dont know where it goes.. it seems to sort the others right..

my compareTo reads:

Code:

       public int compareTo(Object comp)
                {
                        Character first=new Character((this.toString()).charAt(0));
                        Character next=new Character((((AddressEntry) comp).toString()).charAt(0));

                        System.out.println(first+"first");
                        System.out.println(next+"next");
                        int equal=first.compareTo(next);
                        if (equal==0)
                        {
                                Character first2=new Character((this.toString()).charAt(1));
                                Character next2=new Character((((AddressEntry) comp).toString()).charAt(1));
                                equal=first2.compareTo(next2);
                        }
                System.out.println(equal);
                return(equal);
                }



Any help would be appreciated.. thanks
_________________
Signature
Back to top
View user's profile Send private message
Kevin72594
Guru
Guru


Joined: 30 Dec 2003
Posts: 307

PostPosted: Tue Nov 22, 2005 2:29 am    Post subject: Reply with quote

This may be a dumb question, but where exactly does "contents" come from?
Back to top
View user's profile Send private message
abaelinor
n00b
n00b


Joined: 27 Aug 2005
Posts: 51

PostPosted: Tue Nov 22, 2005 2:39 am    Post subject: Reply with quote

aa

Last edited by abaelinor on Tue Oct 21, 2008 1:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
gbkyle
Apprentice
Apprentice


Joined: 29 Nov 2004
Posts: 150
Location: Jersey

PostPosted: Tue Nov 22, 2005 2:54 am    Post subject: Reply with quote

private LinearNode<T> contents;

is defined in my LinkedSet
_________________
Signature
Back to top
View user's profile Send private message
gbkyle
Apprentice
Apprentice


Joined: 29 Nov 2004
Posts: 150
Location: Jersey

PostPosted: Tue Nov 22, 2005 2:55 am    Post subject: Reply with quote

Quote:

as for your code, i've never seen that usage with T (the <T>)

are you talking about "T firstob" or the use of generics?
_________________
Signature
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum