嗨,有人可以帮我解决此功能中使用的排序算法
public void sortList() {
Node current = null, index = null;
int temp;
//Check whether list is empty
if(head == null) {
return;
}
else {
//Current will point to head
for(current = head; current.next != null; current = current.next) {
//Index will point to node next to current
for(index = current.next; index != null; index = index.next) {
//If current's data is greater than index's data, swap the data of current and index
if(current.data > index.data) {
temp = current.data;
current.data = index.data;
index.data = temp;
}
}
}
}
}
顺便说一下,它是双链接列表
当前节点是固定的,然后从下一个节点到末尾进行迭代(通过索引变量),在外循环的一次迭代结束时,当前指向的节点具有正确的值,然后电流进行到下一个节点。这是选择排序,最基本的排序
有趣的事实:尽管由于复杂性而较慢,但在写操作昂贵时可以使用O(n ^ 2)选择排序,因为它最多只交换n次以获取大小为n的列表