I recently took part in ACM certified programming competition. This is the question which I could not do at that time:
“Given an array of integers having n elements, write a program to print all the permutations.”
Please tell me how to do this question. Is there any algorithm to do this kind of questions?
assuming there are no repeats: just change each element with all possible following elements, and recursively invoke the function.
You can see the code with auxilary functions
swap()andprintArray()performing with a basic test case at ideoneBonus: This is similar to the idea of fisher-yates shuffle, but in here – intead to swapping the element at
iwith randomly chosen following element – you swap it with all of them – each at a time.