Skip to content

Instantly share code, notes, and snippets.

@vnkdj5
Created July 1, 2017 04:21
Show Gist options
  • Save vnkdj5/534ff1cf959c0895e435ea11fc3b3e0b to your computer and use it in GitHub Desktop.
Save vnkdj5/534ff1cf959c0895e435ea11fc3b3e0b to your computer and use it in GitHub Desktop.
Permutation Generator
#include<iostream>3
using namespace std;
class PermutationGenerator
{
char *set;
int count=1;
int start,terms,levels;
public:
void swap(char *a,char *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void permute(char *s,int start,int terms,int l=0)
{
int i=0;
if(l==levels)
{
cout<<count<<":";
for(int i=0;i<=levels-1;i++)
cout<<*(s+i);
cout<<endl;
count++;
}
else
{
for(int i=start;i<=terms;i++)
{
swap(s+start,s+i);
permute(s,start+1,terms,l+1);
swap(s+start,s+i);
}
}
}
PermutationGenerator(int t)
{
set=new char[t];
start=0;
terms=t;
levels=0;
}
void getSet()
{
cout<<"ENter Letters: \n";
for(int i=start;i<terms;i++)
cin>>set[i]; // *(set+i);
}
void generatePermutations()
{
cout<<"\nENter Number of literals to be used: ";
cin>>levels;
if(levels<=terms)
permute(set,start,terms-1);
else
cout<<"\nPermutaion Not possible. ";
}
};
int main()
{
int t;
cout<<"Enter Number of elements: ";
cin>>t;
PermutationGenerator p(t);
p.getSet();
p.generatePermutations();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment