This is a question that has already appeared a few times (cf Print all the permutations of a string in C) however what i'm having trouble with is a bit different and i'm having an hard time figuring an answer.
I'm currently working on a function that would apply another function on all permutations of an int array. I decided to use backtracking and it resulted in the code below.
void ft_perm(int *mat, int size, int i) // Initially called with i = 0
{
int j;
j = i;
if (i == size)
{
f(mat, ...) // any function that applies itself on mat
}
else
{
while (j >= 0)
{
ft_swap(mat, i, j); // function that swaps i and j
ft_perm(mat, size, i + 1);
ft_swap(mat, i, j); // backtrack
j--;
}
}
}
However, the order in which the function is applied is not exactly what i'd need and i can't figure out how to make it work correctly. I would need 'f' to be applied in an order where the smallest number is always at the leftmost.
For example, using a 3 number array, the current function will apply 'f' on 012->021->210->102->120 while what i'd need would be to apply 'f' on 012->021->102->201->120->210.
But that's specific, in a nutshell, my question is "Is it possible to manipulate the permutation order using backtracking ?"
Thanks for your help and well, sorry for the long and wobbly question.
Ps : Thanks for the edit !