Recursive permutation in C

Finding all the permutations for any given set of items has always been an area of interest for programmers. A very simple algorithm for finding all the permutations, the Bell Algorithm and its C coding has already been presented.

Programming Geeks : Recursive permutation in C
Here the recursive code in C, for finding all the possible permutations has been given :

#include “stdio.h”
#include “conio.h”
char a[7]={‘a’,'b’,'c’,'d’,'e’,'f’,'g’};
int n=3;
main()
{
int i;
for(i=1;i<=n;i++)
comb(0,i);
}

comb(int index,int number)
{
int i;
char t;
if(index==number) {
for(i=0;i<number;i++)
printf(“%c”,a[i]);
printf(“\n”);
}
else{
for(i=index;i<n;i++)
{
t=a[i];
a[i]=a[index];
a[index]=t;
comb(index+1,number);
a[index]=a[i];
a[i]=t;
}
}
}


Related Posts

  • Share/Bookmark
You can leave a response, or trackback from your own site.

2 Responses to “Recursive permutation in C”

  1. szhaoliang says:

    Thank you for upload the source code.

    ======================================
    #include

    char a[7]={‘a’,'b’,'c’,'d’,'e’,'f’,'g’};
    int n=3;

    void comb(int index,int number);

    int main(void)
    {
    int i;
    for(i=1;i<=n;i++){
    comb(0,i);
    }
    return 0;
    }

    void comb(int index,int number)
    {
    int i;
    char t;
    if(index==number) {
    for(i=0;i<number;i++)
    printf("%c",a[i]);
    printf("\n");
    }
    else{
    for(i=index;i<n;i++)
    {
    t=a[i];
    a[i]=a[index];
    a[index]=t;
    comb(index+1,number);
    a[index]=a[i];
    a[i]=t;
    }
    }
    }

    ======================================
    Save the source and compile it without error and warning.

  2. szhaoliang says:

    #include

    char a[7]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’};
    ……

Leave a Reply

| Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa