How can I implement a permutation program in C?

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on implementing a permutation program in C, specifically for generating permutations of integers up to a maximum of 7. The user has provided a partial implementation using a factorial function and a method to cycle through permutations. Key functions discussed include cycle, add_permutation, and permute, which collectively allow for the generation of all permutations by recursively cycling through the integer array. The user seeks advice on improving the efficiency and structure of their implementation.

PREREQUISITES
  • Understanding of C programming language syntax and structure
  • Knowledge of recursion and its application in algorithm design
  • Familiarity with array manipulation in C
  • Basic understanding of permutations and combinatorial algorithms
NEXT STEPS
  • Research "C recursion best practices" to optimize recursive functions
  • Explore "C dynamic memory allocation" for more efficient array handling
  • Learn about "backtracking algorithms" for generating permutations
  • Investigate "time complexity analysis" for evaluating algorithm efficiency
USEFUL FOR

Software developers, computer science students, and anyone interested in algorithm design and implementation in C, particularly those focusing on combinatorial algorithms and recursion.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
i need to write functions that gives back the permutation of an integer.
i have already the factorial function in, so here what i did so far:
i can assume that MAXCOL is 7, you can assume that n<=7.
Code:
int perm(int n)
{
MAXROW=fact(MAXCOL);
int perms[MAXROW][MAXCOL], i,j;
for(j=0;j<n;j++){
for(i=0;i<fact(n);i++){
perms[i][j]=j+1;
}
}
so far as you can see I am printing the list order i.e if n=2
then
Code:
12
12
now i want to be able to unchange the first line and then go to the second line and change it, and then change the third line that it wouldn't be identical to formers, and so forth.
the problem i don't know how to implement it, obviously some sorting is in place but which?

thanks in advance.
 
Technology news on Phys.org
You want to make a list of all permutations of n integers?

It's not very efficient, but you could use an approach of rotating the first n objects:
Code:
int * cycle (int *numbers, int n) {
   int tmp,i;

   tmp=numbers[0];
   for(i=0;i<n-1;i++) {
      numbers[i]=numbers[i+1];
   }
   numbers[i]=tmp;
   return numbers;
}

/*These are global so that they're static */
int permindex=0;
int permutations[ROWS][COLS]

void add_permutation (int *permutation) {
   int i;
   for(i=0;i<MAXCOLS;i++) {
       permutations[permindex][i]=permutation[i];
   }
   permindex++;
}

int *permute (int *numbers, int n) {
   int i;

   if(1==n) {
      add_permutation(numbers);
   } else {
      for(i=0;i<n;i++) {
         numbers=cycle(numbers,n);
         permute(numbers,n-1);
      }
   }
}

int main {
   int i;
   int numbers[MAXCOLS];

   for(i=0;i<MAXCOLS;i++) {
      numbers[i]=i;
   }
   permute(numbers,MAXCOLS);
}
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K