[英]Insertion Sort algorithm java doubly linked list
I have a doubly linked list with a sentinel node and I need to sort it using Insertion Sort with O(n^2) complexity. 我有一个带有前哨节点的双链表,我需要使用O(n ^ 2)复杂度的插入排序对其进行排序。 I have written this code but it does not work as it is supposed to.
我已经编写了此代码,但是它不能按预期的方式工作。
Any help in general with insertion sort and specifically with a code? 在插入排序方面,特别是在代码方面,总体上有帮助吗?
public void insertionSort()
{
int key,i;
int size = getSize();
this.restart();
for(i = 2; i < size; i++)
{
this.next(); // Go to next node
key = this.getVar(); // get the integer a node holds
while(this.hasNext() == 1 && position.prev.var < key && position.var != -1)
{
position.prev.setNext(position.next);
position.next.setPrev(position.prev);
position.setNext(position.prev);
position.setPrev(position.prev.prev);
position.prev.setNext(position);
position.next.setPrev(position);
this.goBack(); // go to the previous node
}
}
}
Size is how many elements my list has. 大小是列表中有多少个元素。 My sentinel has var = -1 so I want to stop when I am at the head that's why I use this.
我的前哨有var = -1,所以我想停在头上时才停下来,这就是为什么要使用它。
position.var != -1
and this.hasNext() == 1
is true as long as we are at a position != sentinel node . 并且
this.hasNext() == 1
是正确的,只要我们在一个位置!
In the specific example, I have 35 integers in my list: 在特定的示例中,我的列表中有35个整数:
5 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1
5 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1
and I want to sort them this way: 我想用这种方式对它们进行排序:
9 5 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9 5 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
UPDATE: The code I use into the insertion sort is the following: 更新:我在插入排序中使用的代码如下:
public int getSize()
{
int size = 0;
this.restart();
while(this.hasNext() == 1)
{
size++;
this.next();
}
return size;
}
public void restart()
{
position = header;
previous = Sentinel;
}
public void next()
{
previous = position;
position = position.next;
}
public int getVar()
{
return position.var;
}
public void goBack()
{
position = previous;
previous = previous.prev;
}
public int hasNext()
{
if(position.var != -1)
return 1;
else
return 0;
}
public void setNext(Node next)
{
this.next = next;
}
public void setPrev(Node prev) { this.prev = prev; public void setPrev(Node prev){this.prev = prev; }
}
Also, I use a list iterator. 另外,我使用列表迭代器。
This should fix the problem: 这应该可以解决问题:
int j;
// ...
for(i = 1; i < size; i++)
{
this.restart(); // start at ith node
for(j = 0; j < i; j++)
this.next();
key = this.getVar(); // same code as before
or use another variable that advances by one node at a time for each outer loop. 或为每个外部循环使用一次提前一个节点的另一个变量。
Also, shouldn't this.hasNext() be renamed to this.hasPrev() ? 另外,难道不能将this.hasNext()重命名为this.hasPrev()吗?
The main part of the code seems correct, example diagram: 该代码的主要部分似乎是正确的,示例图:
// goal: swap B and C
// initial state
p
-> -> -> ->
A B C D
<- <- <- <-
// remove C from list
position.prev.setNext(position.next);
position.next.setPrev(position.prev);
p
----->
-> -> ->
A B C D
<- <- <-
<----
// update C next and previous
position.setNext(position.prev);
position.setPrev(position.prev.prev);
p
----->
-> -> ->
A C B D
<- <- <-
<----
// insert C into list
position.prev.setNext(position);
position.next.setPrev(position);
p
-> -> -> ->
A C B D
<- <- <- <-
Here's the notes from my analysis of the inner loop, and you were right, there's definitely a problem there. 这是我对内循环的分析中的注释,您说对了,那里肯定有问题。
position.unlink(): step out of line, neighbours become direct neighbours position.unlink():与众不同,邻居成为直接邻居
position.prev.next = position.next; // TODO 1: position.next.prev = position.prev
position.next.prev = position.prev; // TODO 2: position.prev.next = position.next
^ Breaks TODO 1, but we can: position.prev.next.prev = position.prev
So we still need TODO 1 and then 2, in that order: 因此,我们仍然需要TODO 1,然后按此顺序2:
-- TODO 1: position.prev.next.prev = position.prev
-- TODO 2: position.prev.next = position.next
position.insertBefore(position.prev): re-queue one place further back in line position.insertBefore(position.prev):重新排队再排一处
position.next = position.prev; // TODO 3: position.prev.prev = position
position.prev = a = position.prev.prev; // TODO 4: a.next = position
// ^ // same: position.prev.next = position
// | // *Error 1!*
// + breaks TODO's 1 and 2, *Error 2!*
// + breaks TODO 3, but we can instead: position.next.prev = position
Regarding error 1, TODO 3 and TODO 4 both involve position.prev
, setting both it's next
and prev
to position
; 关于错误1,TODO 3和TODO 4都涉及
position.prev
,将它的next
和prev
都设置为position
; this effectively surrounds position.prev
with position
. 这有效地将
position.prev
与position
包围在一起。 No matter if it steps forward or backward, it's direct neighbour will be position
. 没有如果步骤向前或向后的事情,这是直接的邻居将是
position
。 After the one step though, everything is the same. 经过一步,一切都一样。 Interesting structure - it's like the front and back door of your house both access the same spot.
有趣的结构-就像房子的前门和后门都访问同一位置。
Error 2 makes it impossible to update position.prev
, which is needed for TODO 's 1 , 2 and 3 . 错误2使得不可能更新
position.prev
,这是需要TODO的1,2和3。 We can still meet TODO 3 by using the alternate expression accessing a.next
through position.prev
's next
field, but TODO's 1 and 2 can no longer be met. 我们仍然可以通过使用替代表达式通过
position.prev
的next
字段访问a.next
来满足TODO 3 ,但是TODO的1和2将不再满足。
position.insertBefore(position.prev), continued: position.insertBefore(position.prev),继续:
position.prev.next = position; // DONE: 4
position.next.prev = position; // DONE: 3
These two lines complete TODO 3 and 4, so all that's left is: 这两行完成了TODO 3和TODO 4,剩下的就是:
-- TODO 1: position.prev.next.prev = position.prev
-- TODO 2: position.prev.next = position.next
-- TODO 5: fix Error 1
-- TODO 6: fix Error 2
Fixing error 1 involves making sure TODO 1 and 2 are done before TODO 4, and fixing error 2 is making sure TODO 3 is done before a
is assigned. 修正错误1涉及确保TODO 1和2在TODO 4之前完成,修正错误2是确保TODO 3在分配
a
之前完成。
You end up with 8 assignments; 您最终要完成8项作业; in hindsight, not unsurprising for a doubly-linked list and a movement involving 4 nodes.
事后看来,对于双链表和涉及4个节点的运动并不奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.