0
此插入排序屬於雙向鏈接列表。它似乎沒有打印任何東西。對不起,如果這是混亂。發佈內容我相當新。我通過放入sysout來調試它。我相信有一個交換問題,我用sysout &我注意到這是問題發生的地方。任何幫助將不勝感激。我也檢查了我的應用程序類似乎很好。交換插入排序不工作在雙向鏈接列表
public boolean insertionSort()
{
if (getFirst().next != null)
{
return false;
}
Link current = getFirst().next;
Link current2 = current;
while(current != null){
current2 = current;
while(current2.prev != null){
int tempID = Integer.valueOf(current2.Data.getID());
int temp2ID = Integer.valueOf(current2.prev.Data.getID());
if(tempID < temp2ID)
{
swap(current2, current.prev);
}
current2 = current2.prev;
}
current = current.next;
}
return true;
}
public void swap(Link x, Link y)
{
Link previousNode1 = x.prev;
Link nextNode1 = x.next;
Link previousNode2 = y.prev;
Link nextNode2 = y.next;
if (x.next == y || y.next == x)
{
previousNode1.next = y;
y.prev = (previousNode1);
nextNode2.next = (x);
x.next = (nextNode2);
x.prev = (y);
y.next = (x);
}
else
{
y.prev = (previousNode1);
y.next = (nextNode1);
nextNode1.prev = (y);
previousNode1.next = (y);
x.prev = (previousNode2);
x.next = (nextNode2);
nextNode2.prev = (x);
previousNode2.next = (x);
}
} // end swap