A permutation can be obtained by selecting an element in the given set and recursively permuting the remaining elements.
At each stage of the permutation process, the given set of elements consists of two parts: a subset of values that already have been processed, and a subset that still needs to be processed. This logical seperation can be physically realized by exchanging, in the i’th step, the i’th value with the value being chosen at that stage. That approaches leaves the first subset in the first i locations of the outcome.
1 permute(i) 2 if i == N output A[N] 3 else 4 for j = i to N do 5 swap(A[i], A[j]) 6 permute(i+1) 7 swap(A[i], A[j])