Java 插入排序双循环链表

Java 插入排序双循环链表,java,linked-list,circular-list,Java,Linked List,Circular List,所以我想让我的双链表做一个插入排序 我现在在把节点移动到合适的位置时遇到了问题。我已经让比较工作,但我的节点没有移动 public void insertionSort() { Node temp; Node start = this.head.next; Node next = start.next; Node prev = this.head; while(start != this.head) { if(start.data

所以我想让我的双链表做一个插入排序

我现在在把节点移动到合适的位置时遇到了问题。我已经让比较工作,但我的节点没有移动

public void insertionSort()
{
    Node temp;
    Node start = this.head.next;
    Node next = start.next;
    Node prev = this.head;

    while(start != this.head)
    {
        if(start.data.compareTo(next.data) > 0) //start is larger than next
        {
            temp = start;
            start = next;
            next = temp;
        }
        start = start.next;
        next = next.next;
        prev = prev.next;
    }
}
我想知道是否有人能帮我把这个算法弄对。我正在使用循环双链表来测试各种排序例程的时间复杂度。

有趣的谜题

我看到的算法的主要问题是,只给被插入的节点返回一个位置的机会

插入排序从列表中的第二个节点逐个查看最后一个节点,向后交换它们,直到它们在前面已排序的列表中排序为止。这可能需要多次交换,但您的算法只允许一次交换(如果条件为真)或零次交换(如果条件为假)。例如,假设我们正在排序:

b、c、a

start
位于b和c时,插入排序将起作用,然后它将移动到a。这里你说‘如果a在c之前,用c交换’,给出:

b a c

…但是你的算法会终止。由于插入的每个节点需要进行的交换数量不确定,因此需要在那里进行另一个while循环。以下是插入排序算法的伪代码:

function insertion_sort(items) {
    while (i=1; i < items length; i++) { // for elements 1 to n
        while (j=i; j >= 0; j--) { // look backward through all previously sorted items
            if (items element at j < items element at j-1) {
                 swap element at j with element at j-1
            } else {
                break out of the loop
            }
        }
    }
}
函数插入\u排序(项){
而(i=1;i=0;j--){//向后查看所有先前排序的项
if(j处的项目元素
循环列表是如何排序的?为什么不使用“插入前”或“插入后”来代替交换?如果使用“插入前”和“插入后”会更容易吗?