programming-examples/c++/Combinational_Problems/C++ Program to Generate All Permutations using BackTracking.cpp
2019-11-15 12:59:38 +01:00

70 lines
928 B
C++

/*
* C++ Program to Generate All Permutations using BackTracking
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
/* swap values at two pointers */
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* print permutations of string */
void permute(char *a, int i, int n)
{
int j;
if (i == n)
cout<<a<<endl;
else
{
for (j = i; j <= n; j++)
{
swap((a + i), (a + j));
permute(a, i + 1, n);
swap((a+i), (a + j));
}
}
}
/* Main*/
int main()
{
char a[] = "abcd";
permute(a, 0, 3);
return 0;
}
/*
abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc