Print all possible permutation of array elements - Algorithms Tutorial
Problem :-Print all possible permutation of array elements.
Consider the array { 3, 2, 5 }.
Possible permutations : { 3, 2, 5 } | { 3, 5, 2 } | { 2, 3, 5 } | { 2, 5, 3 } | { 5, 2, 3 } | { 5, 3, 2 }
Solution :-
An array of length n has n ! permutations.
The implementation show below uses a recursive approach with swapping of array elements to obtain possible permutations and then swapping of elements in the permuted arrays to obtain further permutations.
We iterate over the array elements and at each iteration level, we do the swap and permute step described above. After the jth iteration, j elements in the obtained permuted arrays are fixed and we move to the next iteration.
Read full article from Print all possible permutation of array elements - Algorithms Tutorial
No comments:
Post a Comment