*EDIT* Here I go on about how order matters, but I neglected to ask if it's that particular output that you require? The example I provided sorts based on lexographical ascending order.
The algorithm which in the link above is called "University of Exeter algorithm" is quite easy to understand. (Note that it does not take care of duplicate elements in the array.)
#include <iostream>
#include <iomanip>
void swap( int a[], int i, int j ) // swap elements a[i], a[j]
{
int temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
void print_array( constint a[], int n )
{
staticint cnt = 0 ;
std::cout << std::setw(3) << ++cnt << ". " ;
for( int i = 0 ; i < n ; ++i ) std::cout << a[i] << ' ' ;
std::cout << '\n' ;
}
// array 'a' of size 'n', permute range starting at position 'from'
void permute( int a[], int n, int from = 0 )
{
// if the entire range has been permuted, print the permutation
if( from == n ) return print_array( a, n ) ;
// else
for( int i = from ; i < n ; ++i ) // repeat: for each element in the range to permute
{
swap( a, from, i ) ; // make this element the head (at position from)
permute( a, n, from+1 ) ; // form all permutations of the tail (starting at from+1)
swap( a, from, i ) ; // restore the original positions
}
}
int main()
{
int a[] = { 1, 2, 3, 4 } ;
std::cout << "permutations\n--------------\n" ;
permute( a, sizeof(a) / sizeof( a[0] ) ) ;
}
JlBorges thank for your contribution, thats what i was trying to do too with swap function but i needed to go as far as you i needed permutation function.
if anyone else got other way to show me how to do it, ill apreciate it