Analysis:
A basic interview question on recursion.
Solution:
#include <stdio.h>
#include <string.h>
void permutation(char *s, int i, int n)
{
if(i == n)
{
printf("%s\n", s);
return;
}
int j;
char temp;
for(j = i; j < n; j++)
{
temp = s[j];
s[j] = s[i];
s[i] = temp;
permutation(s, i+1, n);
temp = s[j];
s[j] = s[i];
s[i] = temp;
}
}
int main(void) {
char s[] = "abc";
int len = strlen(s);
permutation(s, 0, len);
return 0;
}
No comments:
Post a Comment