我试图编写一个排序链表的方法。 这是对我的Java培训。JAVA - 按降序对链表进行排序
该方法应该得到一个带有值的链表,并用选择排序。 但不是通常的选择排序,而是选择排序,找到最大的数字,并将其放在链接列表的开始。直到列表排序。
我试着按照调试器,但是,我真的不明白我做错了什么。
这是我累了:
public IntList selectionSort()
{
IntNode tempMax = _head;
IntNode current = _head;
IntNode fromHere = null;
IntNode toHere = _head;
IntNode prev = null;
while(toHere != null)
{
current = toHere;
tempMax = toHere;
while (current != null)
{
if (current.getNext() != null && current.getNext().getValue() > tempMax.getValue())
{
prev = current;
tempMax = current.getNext();
current = current.getNext();
}
else current = current.getNext();
}
prev.setNext(prev.getNext().getNext());
tempMax.setNext(toHere);
if (fromHere == null)
_head = tempMax;
else fromHere.setNext(tempMax);
fromHere = tempMax;
toHere = fromHere.getNext();
}
return this;
}
为什么不移动'IntNode'内的值而不是移动'IntNode'。 – UmNyobe 2012-02-07 12:33:21
你可以在你的代码中加入一些注释吗?我很难跟踪哪些变量用于什么。 – 2012-02-07 13:01:00