I got following algorithm for finding permutation but I can't code it.
Can any one can code this permutation algorithm in c++ using recursive method as below
bool nextPermutation(int array[])

1. The first permutation is the permutation represented by the sequence (1, 2, …, n).
2. The last permutation is the permutation represented by the sequence (n, …, 2, 1).
3. If is an arbitrary permutation, then the “next” permutation is produced by the following procedure:
(i) If the maximal element of the array (which is n) is not in the first position of the array, say , where , then swap and . This will give you the “next” permutation in this case.
(ii) If the maximal element of the array is in the first position, so , then to find the “next” permutation to the permutation , first find the “next” permutation to , and then add to the end of thus obtained array of (n-1) elements.
(iii) Consecutively applying this algorithm to permutations starting from (1, 2, …, n), you will eventually list all possible permutations. The last one will be (n, …, 2, 1).


input array is of 10 element using 1,2,3,4,5,6,7,8,9,10 in any order but not repeat.
for array of a[4]={1,2,3,4} out put shold be

1234 1243 1423 4123 1324 1342 1432 4132 3124 3142 3412 4312 2134 2143 2413 4213 2314 2341 2431 4231 3214 3241 3421 4321 = 4!=24

I am trying this since 4 days but nothing.........

Please help...................

Member Avatar for iamthwee

Have you tried looking in the code snippet section. There might be something useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.