我有一个伪代码算法,它将一个未排序的数组作为输入并将其排序在一个链表中。我不太明白它是如何工作的。也许其中有一个错误。我已经尝试了一个多小时来了解它是如何工作的,但我被困在某个点上。我会尽可能多地解释。任何知道它是如何工作的人都很乐意提供帮助。
ORDER(A)
head [L1] = create(); //Null List is created.
key[head[L1]] = A[1]; //First element of the array is put as the first in list.
for i=2 to length(A);
x = create(); // create a node for every element of the array
key[x] = A[i]; // put A[i] as key in the node.
p1 = head[L1]; // make p1 head of the list.
if key[p1] > A[i]; //compare A[i] with the head
insert_head(x); // if smaller put it in the beggining and make it head
while key[next[p1]] < A[i] AND next[p1] =/= nil // This part I don't understand
if next[p1] == nil //very well.
next[p1] = x
next[x] = nil
else
next[x] = next[p1]
next[p1] = x;