[英]Quicksort Linked List Pivot Last Element
I'm trying to implement a quicksort for sorting a CYCLIC LINKED LIST! 我正在尝试实现一种快速排序,以对循环链接列表进行排序!
There is a DoublyLinkedList
which contains ListElements
. 有一个包含
ListElements
的DoublyLinkedList
。
The ListElement
has the Element First, which has a reference to its predecessor and successor. ListElement
具有Element First,该元素引用其前任和后继。
eg DoubleLinkyedList
in contains the ListElement
first. 例如
DoubleLinkyedList
中首先包含ListElement
。 First can be referenced with first.prev oder first.next. 首先可以用first.prev或first.next引用。
The last Element in the Linked List has a reference to the first element so eg last = first.prev
; 链表中的最后一个元素具有对第一个元素的引用,因此例如
last = first.prev
;
My problem is, I keep getting an error, but I don't know which one precisely, cause it's like a never ending loop and the terminal is not printing the precise exception. 我的问题是,我不断收到错误,但是我不知道确切是哪个错误,这导致它就像一个永无休止的循环,并且终端没有输出精确的异常。
My code of my class Sorter.java where I implement the sorting operation: 我的类Sorter.java的代码,在其中实现了排序操作:
package ads1ss13.pa;
public class Sorter {
public DoublyLinkedList quicksort(DoublyLinkedList in, int numOfElements) {
System.out.println("STARTE sort() in quicksorT()");
in = Sort(in, in.first.getId(), in.first.prev.getId());
return in;
}
public DoublyLinkedList Sort(DoublyLinkedList in, int l, int r)
{
System.out.println("Sort()");
l = in.first.getId();
r = in.first.prev.getId();
ListElement pivot = null;
System.out.println("l: "+l+"; r: "+r);
if(l < r)
{
pivot = in.first.prev;
}
System.out.println("pivot: "+pivot);
System.out.println("Ausfuehren der Partition mit l, r, pivot");
int p = Partition(in, l, r, pivot);
System.out.println("Ausgabe aus Partition p: "+p);
System.out.println("Ausführen der Sort() mit l und p-1 für r");
Sort(in, l, p-1);
System.out.println("Ausführen der Sort() mit p+1 für l, und r");
Sort(in, p+1, r);
return in;
}
public int Partition(DoublyLinkedList in, int l, int r, ListElement pivot)
{
System.out.println("Partition()");
int i = l;
int j = r;
ListElement r_ListElement = in.first.prev;
ListElement i_element = in.first.next;
ListElement j_element = pivot;
System.out.println("i: "+i+"; j: "+j+", pivot: "+j_element);
do {
do {
i = i + 1;
i_element = i_element.next;
} while (i_element.getKey() < pivot.getKey());
do {
j = j - 1;
j_element = j_element.prev;
} while (j > i || j_element.getKey() > pivot.getKey());
if(i_element.getKey() < j_element.getKey())
{
swap(in, i_element, j_element);
}
} while (i < j);
swap(in, i_element, r_ListElement);
return i;
}
public void swap(DoublyLinkedList in, ListElement links, ListElement rechts)
{
if(links.next.getId()==rechts.getId()){
ListElement Lprev = links.prev;
ListElement Rnext = rechts.next;
Lprev.next=rechts;
rechts.prev=Lprev;
links.next=Rnext;
Rnext.prev=links;
links.prev=rechts;
rechts.next=links;
}
else if(rechts.next.getId()==links.getId())
{
swap(in, rechts, links);
}
else
{
ListElement Lprev=links.prev;
ListElement Lnext = links.next;
links.next=rechts.next;
links.prev=rechts.prev;
rechts.next=Lnext;
rechts.prev=Lprev;
links.prev.next=links;
links.next.prev=links;
rechts.prev.next=rechts;
rechts.next.prev=rechts;
}
if(in.first.getId()==links.getId())
{
in.first = rechts;
}
else if(in.first.getId()==rechts.getId())
{
in.first = links;
}
}
}
Do you have any idea why my code never switches to the second recursive call of sort()
and which error is returned? 您是否知道为什么我的代码从不切换到
sort()
的第二个递归调用,并且返回哪个错误?
This is hard to analyze because I can't throw the code into a debugger. 这很难分析,因为我无法将代码扔到调试器中。 However, a few things to get you on the right track:
但是,请注意以下几点:
You take int l
and int r
as arguments, yet you immediately overwrite them: 您将
int l
和int r
作为参数,但立即覆盖了它们:
l = in.first.getId();
r = in.first.prev.getId();
This is almost certainly not what you want to do, as you will restart over and over again. 这几乎肯定不是您想要执行的操作,因为您将一遍又一遍地重新启动。 This is probably what's causing your infinite loop.
这可能是导致无限循环的原因。
if(l < r)
{
pivot = in.first.prev;
}
If l >= r
, pivot is null
, which will cause a NullPointerException
如果
l >= r
,pivot为null
,则将导致NullPointerException
Your partition logic is wrong. 您的分区逻辑是错误的。 You don't need to have a nested loop.
您不需要嵌套循环。 As soon as you compare the element to the pivot, you can immediately do a swap (you will always know which side of the pivot it is supposed to be on).
将元素与枢轴进行比较后,就可以立即进行交换(您将始终知道它应该位于枢轴的哪一侧)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.