selection sort on singly linked list
my algorithm works...almost...for some reason the last two elements are in the wrong order after sorting. Also, the print statement does not show me that first
moves to the right.
public static SLL sort(SLL list)
{
SLLNode first = list.first ;
SLLNode second ;
while (first!=null)
{
System.out.println(first.data); // for seeing if first does move to the right
second = first.succ ;
while (second!=null)
{
if (second.data.compareTo(first.data)<0)
{
开发者_运维百科 String temp = first.data ;
first.data = second.data ;
second.data = temp ;
}
second = second.succ ;
}
first = first.succ ;
}
return list ;
}
before sorting: FFF fff Hi AAA Bye Ciao
after sorting: AAA Bye Ciao FFF Hi fff
my print statement outputs FFF
at first, then only fff
's
Because ascii value of capitals is less than that of lowercase letters.
Also, the print statement does not show me that first moves to the right. System.out.print(); // for seeing if first does move to the right
That print() statement just shows nothing.
精彩评论