Skip to content

Instantly share code, notes, and snippets.

@VitorDiToro
Created September 20, 2016 17:44
Show Gist options
  • Save VitorDiToro/2c47abd2c70a346485fdce4354405149 to your computer and use it in GitHub Desktop.
Save VitorDiToro/2c47abd2c70a346485fdce4354405149 to your computer and use it in GitHub Desktop.
recursivePermutation.c
#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