#include<stdio.h>
#include<algorithm>
using namespace std;
void swap(int &a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
void permutation(int array[], int start, int end)
{
if (array == NULL) return;
if (start == end)
{
for_each(array, array+end+1, [] (int i)-> void { printf("%d",i); });
printf("\n");
}
for (int j = start; j <= end; j++)
{
int k;
for (k = 0; k < j; k++)
{
if (array[k] == array[j])
break;
}
if (k<j) continue;
swap(array[j],array[start]);
permutation(array, start+1, end);
swap(array[j],array[start]);
}
}
void reverse(int array[], int start, int end)
{
while( start < end )
{
swap( array[start++], array[end--] );
}
}
void permutation2(int array[],int length)
{
sort(array,array+length,[](int x, int y ) -> bool {return x<y;});
while (true)
{
for (int j = 0; j < length; j++)
{
printf("%d",array[j]);
}
printf("\n");
int i = length - 2;
for (;i >= 0 && array[i] >= array[i+1]; i--);
if (i < 0) return;
int k = length-1;
for (;k > i && array[k] <= array[i]; k--);
swap(array[i],array[k]);
reverse(array, i+1, length-1);
}
}
void permutation3(int array[], int length)
{
sort(array,array+length,[](int x, int y ) -> bool {return x<y;});
do
{
for_each(array,array+length,[](int x) -> void{ printf("%d",x); });
printf("\n");
}while(next_permutation(array, array+length));
}
int main()
{
int array[] = {2,1,4,3};
permutation3(array, 4);
return 0;
}