I want to print all permutation of string in lexicographic order. I wrote this code:
void permute(char *a, int i, int n) {
if (i == (n-1)) printf("\"%s\"\n", a);
else {
for (int j = i; j < n; j++) {
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
}
}
}
Let's take for example string abc
, I want to receive all permutation in lexicographic order as in the left column, but I have result as in the right column.
"abc" "abc"
"acb" "acb"
"bac" "bac"
"bca" "bca"
"cab" <
"cba" "cba"
> "cab"
Can someone help me with this? I saw some algorithms, but they look difficult. I think I can save all generated strings in an array and then sort this array, but I cannot write this (I'm a beginner in C).