I'm trying to understand Heap's algorithm from the Wikipedia page and I'm trying to compare the picture with the algorithm and I can't seem to figure it out
this picture is from the wikipedia page
why would it switch the #1 and #2 first, shouldn't it switch the #1 and #4 first?
I'm using java but this is just the code copied from Wikipedia, I understand that there is switching involved for the code in general
if k = 1 then output(A) else // Generate permutations with kth unaltered // Initially k == length(A) generate(k - 1, A) // Generate permutations for kth swapped with each k-1 initial for i := 0; i < k-1; i += 1 do // Swap choice dependent on parity of k (even or odd) if k is even then swap(A[i], A[k-1]) // zero-indexed, the kth is at k-1 else swap(A[0], A[k-1]) end if generate(k - 1, A) end for end if
k
is initially 4 so wouldn't it switch A[0] and A[3] first?
Sorry in advance if this is a stupid question...
https://stackoverflow.com/questions/66576314/heap-s-algorithm-wiki-picture-vs-algorithm March 11, 2021 at 12:06PM
没有评论:
发表评论