#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void sort(int *a, int start, int end)
{
int i;
if(start == end)
{
for(i=0; i<=end; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(i=start; i<=end; i++)
{
swap(a+start, a+i);
sort(a, start+1, end);
swap(a+start, a+i);
}
}
}
int main(int argc, char** argv)
{
int a[4] = {1, 2, 3, 4};
sort(a, 0, 3);
return 0;
}
jl@jl-virtual-machine:~/test$ ./a.out
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
jl@jl-virtual-machine:~/test$