开发者

selection sort on singly linked list

开发者 https://www.devze.com 2023-02-20 02:59 出处:网络
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.

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.

selection sort on singly linked list


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消