let a[] represent an arbitrary list of objects to permute
let N equal the length of a[]
create an integer array p[] of size N to control the iteration
initialize p[0] to 0, p[1] to 1, p[2] to 2, ..., p[N-1] to N-1
initialize index variable i to 1
while (i < N) do {
if (p[i] is greater than 0) then {
decrement p[i] by 1
let j = 0
if i is odd, then let j = p[i]
swap(a[j], a[i])
let i = 1
} // end if
else { // (p[i] equals 0)
let p[i] = i
increment i by 1
} // end else (p[i] equals 0)
} // end while (i < N)
|