| View previous topic :: View next topic |
| Author |
Message |
gbkyle Apprentice


Joined: 29 Nov 2004 Posts: 150 Location: Jersey
|
Posted: Mon Nov 21, 2005 11:59 pm Post subject: Java Programming Question, selectionSort doubleylinkedlist |
|
|
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 |
|
 |
Kevin72594 Guru


Joined: 30 Dec 2003 Posts: 307
|
Posted: Tue Nov 22, 2005 2:29 am Post subject: |
|
|
| This may be a dumb question, but where exactly does "contents" come from? |
|
| Back to top |
|
 |
abaelinor n00b

Joined: 27 Aug 2005 Posts: 51
|
Posted: Tue Nov 22, 2005 2:39 am Post subject: |
|
|
aa
Last edited by abaelinor on Tue Oct 21, 2008 1:41 pm; edited 1 time in total |
|
| Back to top |
|
 |
gbkyle Apprentice


Joined: 29 Nov 2004 Posts: 150 Location: Jersey
|
Posted: Tue Nov 22, 2005 2:54 am Post subject: |
|
|
private LinearNode<T> contents;
is defined in my LinkedSet _________________ Signature |
|
| Back to top |
|
 |
gbkyle Apprentice


Joined: 29 Nov 2004 Posts: 150 Location: Jersey
|
Posted: Tue Nov 22, 2005 2:55 am Post subject: |
|
|
| 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 |
|
 |
|