Here is simple code to generate permutations
#include "stdafx.h"
#include<stdio.h>
int Permute( char *, int);
int PrintArray();
int swap(char *, int);
char A[]={'1','2','3','4','5','6','7','8'};
main( )
{
Permute(A,sizeof(A)/sizeof(char));
return 0;
}
int Permute(char * a, int n)
{ if(n==1)
{ PrintArray();
return 0;
}
int i=0;
for(i=0; i<n;i++)
{
swap(a,i);
Permute(a+1,n-1);
swap(a,i);
}
}
int PrintArray()
{
int i=0;
static int Number_of_Solutions=0;
printf("\n Conf: %-4d ",++Number_of_Solutions);
for(i=0;i<sizeof(A);i++)
printf("%c ",A[i]);
return 0;
}
int swap( char *a, int i)
{
if(i<0)
return -1;
int temp=*a;
*a=a[i];
a[i]=temp;
return 0;
}
Regards,
Bujji
On Aug 1, 4:00 pm, UMESH KUMAR <[email protected]> wrote:
> Write a C code for generate all possible Permutation
> as:- 1 2 3
> Total no. of Per=6
> also print all permutation
> as:- 1 2 3
> 1 3 2
> 2 1 3
> 2 3 1
> 3 1 2
> 3 2 1
> if inpute is 1 2 3 2
> total no of permutation = 12
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.