This question has already been answered:

For example, there is an array var mas = int[1,2,3,4,5,...,n] . The number of options for the location of elements will be n! . How to get all possible options?

Reported as a duplicate by participants iksuy , Dmitry Maslennikov , kizoso , Harry , ߊߚߤߘ 26 Oct '17 at 22:37 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Simple iterative implementation of the algorithm in C ++

     #include <iostream> using namespace std; int main() { const int n=4; int a[n]; int i,j,k,i1,i2; int temp; for(i=0; i<n; i++) a[i]=i+1; for(;;) { for(i=0; i<n; i++) cout << a[i] << " "; cout << '\n'; for(j=n-1; j>0; j--) if(a[j]>a[j-1]) break; if(j==0) return 0; j--; for(k=n-1; k>j; k--) { if(a[k]>a[j]) { temp=a[k]; a[k]=a[j]; a[j]=temp; break; } } for(i1=j+1,i2=n-1; i1<i2; i1++,i2--) { temp=a[i1]; a[i1]=a[i2]; a[i2]=temp; } } }