# Thread: what a big work for me!!!

1. ## what a big work for me!!!

Hi, I just tried to display a permuttion of n number, i.e. 1,2,3,4. the I want to disply this permutation
using C++ code, I just can't figure out the correct algorithum to manupulat it.
the result should be looks like;
1,2,3,4
1,2,4,3
1,3,2,4
1,3,4,2
1,4,2,3
1,4,3,2
......
the total display lines should be 24 (=4!). could some one figure out the algorithum?

thanks;

2. Well, a VERY CRUDE method would be to make a counter that counts from 0 to 9999, and then display only the numbers that match the criteria (i.e. throw-away any results containing a zero or a digit greater than 5, and any results containing duplicate numbers.

An improvement would be a base-n counter that starts at 1234 and throws-away (or skips) duplicated digits.

3. I just realized that if n>9, then you'll HAVE to build your own counter (in an array). There must be a proper algorithm for this(?)

4. You should be able to use recursion.

5. Actually, I tried to use the recursive method, but I didn't get the correct result, I did try the loop method,I am just confused by how the elements should be exchanged in the array( I use an array to hold the elements), and use the swap function to exchange the elements in each loop.because the result from the total loop(24 for 4 elements) did not generate all the possible permutation.