我有一個僞代碼算法,它採用未排序的數組作爲輸入,並將其排序在鏈表中。我不太清楚它是如何工作的。也許它有一個錯誤。我一直在嘗試了一個多小時,以瞭解它是如何工作的,但我在某個時刻被卡住了。我會盡我所能解釋。任何知道它如何工作的人都樂意提供幫助。排序一個數組,並把它放在一個鏈表
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;
'對一個數組進行排序並將其放入一個鏈表中或'將一個未排序的數組放入一個已排序的鏈表中? – sircodesalot
取一個未排序的數組,並將其放入已排序的鏈接列表中。 –