A program to find out all possible Permutations.
If I/P is 3 then o/p is
Permute[ 0] 1 2 3
Permute[ 1] 1 3 2
Permute[ 2] 2 1 3
Permute[ 3] 2 3 1
Permute[ 4] 3 2 1
Permute[ 5] 3 1 2
**********************************************************************************/
void permute(int i)
{
int j=0,k=0,temp=0;
if(i == size)
{
return;
}
for(j = i; j < size; j++)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
permute(i+1);
if(i == size - 1)
{
printf("Permute[%3d]",count++);
for(k = 0;k< size;k++)
printf(" %3d ",arr[k]);
printf("\n");
}
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
main()
{
int i = 0;
size = 3;
count = 0;
arr = (int *)malloc(size*sizeof(int));
for(i=0;iarr[i] = i+1;
permute(0);
}
No comments:
Post a Comment