Created
September 20, 2016 17:44
-
-
Save VitorDiToro/2c47abd2c70a346485fdce4354405149 to your computer and use it in GitHub Desktop.
recursivePermutation.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void swap(char* x, char* y ); | |
void permute(char* str, unsigned int l, unsigned int s); | |
int main() | |
{ | |
char str[] = "ABCDA"; | |
unsigned int n = strlen(str); | |
permute(str, 1, n-2); | |
getchar(); | |
return 0; | |
} | |
void swap(char* x, char* y) | |
{ | |
char tmp; | |
tmp = *x; | |
*x = *y; | |
*y = tmp; | |
} | |
void permute(char* str, unsigned int l, unsigned int s) | |
{ | |
int i; | |
if (l == s) | |
printf("%s\n", str); | |
else | |
{ | |
for (i = l; i <= s; i++) | |
{ | |
swap((str+l), (str+i)); | |
permute(str, l+1, s); | |
swap((str+l), (str+i)); //backtrack | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment