permutations.c
void permutations(int set[], int target[], int n, int r, int level) {
   int n_l, r_l;
   int set_indx, tmp_indx, cnt;
   int *tmp_array;
   int target_offset;
   size_t block_size;
   char *indent;
   unsigned b4;

   n_l = n - level;
   r_l = r - level;
   block_size = (2 * level + 1) * sizeof(char);
   indent = (char *)malloc(block_size);
   memset(indent,' ', block_size-1);
   indent[block_size-1] = 0;

   printf("%spermutations(", indent);
   print_permutation(set, n_l, 1);
   b4 = (unsigned int)0xFFFF & (unsigned int)target;
   printf(", 0x%04x, %i, %i, %i) {\n", b4, n, r, level);

   if (r_l<1) return ;
   // block_size = (n_l * r_l) * sizeof(int);

   if (r_l==1){
      for (set_indx=0; set_indx<n_l; set_indx++) {
         target_offset = r * set_indx + level;
         target[target_offset] = set[set_indx];
         b4 = (unsigned int)0xFFFF & (unsigned int)(target + target_offset);
   printf("%s1 PUT value %i at 0x%04x\n", indent, set[set_indx], b4);
   printf("%s1   Indx %i offset is: %i with value %i\n", indent, set_indx, target_offset, set[set_indx]);
      }
   printf("%s1   return; }\n", indent);
   free(indent);
      return ;
   }

   level++;
   // allocate temporary memory
   block_size = (n_l - 1) * sizeof(int);
   tmp_array = (int *)malloc(block_size);
   for (set_indx=0; set_indx<n_l; set_indx++) {
      // put each element to start
      for (cnt=0; cnt<n_l-1; cnt++) {
         target_offset = r * ((n_l - 1) * set_indx + cnt);
         target[target_offset] = set[set_indx];
         b4 = (unsigned int)0xFFFF & (unsigned int)(target + target_offset);
   printf("%s2 PUT value %i at 0x%04x\n", indent, set[set_indx], b4);
   printf("%s2   Indx %i offset is: %i with value %i\n", indent, set_indx, target_offset, set[set_indx]);
      }
         // target[r * ((n_l - 1) * indx + cnt)] = set[set_indx];
      // permute the remaining spaces
         // first, save the remaining numbers to the tmp memory
      for (cnt=0, tmp_indx=0; cnt<n_l; cnt++) {
         if (cnt == set_indx) continue;
         tmp_array[tmp_indx] = set[cnt];
         tmp_indx++;
      }
         // second, recurse to do the remainding numbers
      // target_offset = r * set_indx;
      // target_offset = r * n_l * set_indx;
      target_offset = r * (n - 1) * set_indx;
      b4 = (unsigned int)0xFFFF & (unsigned int)target;
   printf("%s3 Indx %i with target 0x%04x + offset %i\n", indent, set_indx, b4, target_offset);
      permutations(tmp_array, target + target_offset, n, r, level);
   }
   // clean up and return
   printf("%s4 return; }\n", indent);
   free(tmp_array);
   free(indent);
   return ;
}
/*
The number of permutations of n objects taken r at a time
(with repetitions not allowed) is determined as: 
   P(n, r) = (n!) / ((n-r)!)
Parameters:
   set is an array of integers to be permuted.
   target is the memory location to hold the results of the permutations.
   n is the number of objects.
   r is the number of places that they can go into.
   level is the level of recursion starting with zero.
*/
The number of permutations of n objects taken r at a time
(with repetitions not allowed) is determined as:
P(n, r) = (n!) / ((n-r)!)
Parameters:
set is an array of integers to be permuted.
target is the memory location to hold the results of the permutations.
n is the number of objects.
r is the number of places that they can go into.
level is the level of recursion starting with zero.
The number of permutations of n objects taken r at a time ( with repetitions not allowed ) is determined as : P ( n r ) = ( n! ) / (( n - r ) ! ) Parameters : set is an array of integers to be permuted . target is the memory location to hold the results of the permutations . n is the number of objects . r is the number of places that they can go into . level is the level of recursion starting with zero .
void permutations(int set[], int target[], int n, int r, int level) {
   int n_l, r_l;
   int set_indx, tmp_indx, cnt;
   int *tmp_array;
   int target_offset;
   size_t block_size;
   char *indent;
   unsigned b4;
   n_l = n - level;
   r_l = r - level;
   block_size = (2 * level + 1) * sizeof(char);
   indent = (char *)malloc(block_size);
   memset(indent,' ', block_size-1);
   indent[block_size-1] = 0;
   printf("%spermutations(", indent);
   print_permutation(set, n_l, 1);
   b4 = (unsigned int)0xFFFF & (unsigned int)target;
   printf(", 0x%04x, %i, %i, %i) {\n", b4, n, r, level);
   if (r_l<1) return ;
   if (r_l==1){
      for (set_indx=0; set_indx<n_l; set_indx++) {
         target_offset = r * set_indx + level;
         target[target_offset] = set[set_indx];
         b4 = (unsigned int)0xFFFF & (unsigned int)(target + target_offset);
   printf("%s1 PUT value %i at 0x%04x\n", indent, set[set_indx], b4);
   printf("%s1   Indx %i offset is: %i with value %i\n", indent, set_indx, target_offset, set[set_indx]);
      }
   printf("%s1   return; }\n", indent);
   free(indent);
      return ;
   }
   level++;
   block_size = (n_l - 1) * sizeof(int);
   tmp_array = (int *)malloc(block_size);
   for (set_indx=0; set_indx<n_l; set_indx++) {
      for (cnt=0; cnt<n_l-1; cnt++) {
         target_offset = r * ((n_l - 1) * set_indx + cnt);
         target[target_offset] = set[set_indx];
         b4 = (unsigned int)0xFFFF & (unsigned int)(target + target_offset);
   printf("%s2 PUT value %i at 0x%04x\n", indent, set[set_indx], b4);
   printf("%s2   Indx %i offset is: %i with value %i\n", indent, set_indx, target_offset, set[set_indx]);
      }
      for (cnt=0, tmp_indx=0; cnt<n_l; cnt++) {
         if (cnt == set_indx) continue;
         tmp_array[tmp_indx] = set[cnt];
         tmp_indx++;
      }
      target_offset = r * (n - 1) * set_indx;
      b4 = (unsigned int)0xFFFF & (unsigned int)target;
   printf("%s3 Indx %i with target 0x%04x + offset %i\n", indent, set_indx, b4, target_offset);
      permutations(tmp_array, target + target_offset, n, r, level);
   }
   printf("%s4 return; }\n", indent);
   free(tmp_array);
   free(indent);
   return ;
}
void permutations ( int set [ ] , int target [ ] , int n , int r , int level ) { int n_l , r_l ; int set_indx , tmp_indx , cnt ; int * tmp_array ; int target_offset ; size_t block_size ; char * indent ; unsigned b4 ; n_l = n - level ; r_l = r - level ; block_size = ( 2 * level + 1 ) * sizeof ( char ) ; indent = ( char * ) malloc ( block_size ) ; memset ( indent , ' ' , block_size - 1 ) ; indent [ block_size - 1 ] = 0 ; printf ( " " , indent ) ; print_permutation ( set , n_l , 1 ) ; b4 = ( unsigned int ) 0xFFFF & ( unsigned int ) target ; printf ( " \n " , b4 , n , r , level ) ; if ( r_l < 1 ) return ; if ( r_l == 1 ) { for ( set_indx = 0 ; set_indx < n_l ; set_indx ++ ) { target_offset = r * set_indx + level ; target [ target_offset ] = set [ set_indx ] ; b4 = ( unsigned int ) 0xFFFF & ( unsigned int ) ( target + target_offset ) ; printf ( " \n " , indent , set [ set_indx ] , b4 ) ; printf ( " \n " , indent , set_indx , target_offset , set [ set_indx ] ) ; } printf ( " \n " , indent ) ; free ( indent ) ; return ; } level ++ ; block_size = ( n_l - 1 ) * sizeof ( int ) ; tmp_array = ( int * ) malloc ( block_size ) ; for ( set_indx = 0 ; set_indx < n_l ; set_indx ++ ) { for ( cnt = 0 ; cnt < n_l - 1 ; cnt ++ ) { target_offset = r * ( ( n_l - 1 ) * set_indx + cnt ) ; target [ target_offset ] = set [ set_indx ] ; b4 = ( unsigned int ) 0xFFFF & ( unsigned int ) ( target + target_offset ) ; printf ( " \n " , indent , set [ set_indx ] , b4 ) ; printf ( " \n " , indent , set_indx , target_offset , set [ set_indx ] ) ; } for ( cnt = 0 , tmp_indx = 0 ; cnt < n_l ; cnt ++ ) { if ( cnt == set_indx ) continue ; tmp_array [ tmp_indx ] = set [ cnt ] ; tmp_indx ++ ; } target_offset = r * ( n - 1 ) * set_indx ; b4 = ( unsigned int ) 0xFFFF & ( unsigned int ) target ; printf ( " \n " , indent , set_indx , b4 , target_offset ) ; permutations ( tmp_array , target + target_offset , n , r , level ) ; } printf ( " \n " , indent ) ; free ( tmp_array ) ; free ( indent ) ; return ; }
The number of permutations of n objects taken r at a time
(with repetitions not allowed) is determined as:
P(n, r) = (n!)
The number of permutations of n objects taken r at a time ( with repetitions not allowed ) is determined as : P ( n r ) = ( n! )
// block_size = (n_l * r_l) * sizeof(int); // allocate temporary memory // put each element to start // target[r * ((n_l - 1) * indx + cnt)] = set[set_indx]; // permute the remaining spaces // first, save the remaining numbers to the tmp memory // second, recurse to do the remainding numbers // target_offset = r * set_indx; // target_offset = r * n_l * set_indx; // clean up and return