[Recursive] recursive algorithm for sorting columns n full of different characters

. 1 #include <stdio.h>
 2  / * 
. 3      in Example ABCD:
 4      1. Fixed A, BCD recursion whole arrangement until the left one character, all characters printed. Restore the original order.
. 5      2.A and B exchanges, fixed B, recursion ACD whole arrangement. Restore the original order.
6      3. exchange request until the full array of D have been exhausted.
. 7  * / 
. 8  void Perm ( char STR [], int K, int n-) {
 . 9      int I;
 10      char TEMP;
 . 11      IF (K == n-) {
 12 is          for (I = 0 ; I <n-; ++ I )    
 13              printf ( " % c ", str[i]);
14         printf("\n");
15     }else{
16         for(i = k; i < n; ++i){
17             temp = str[k];
18             str[k] = str[i];
19             str[i] = temp;
20             perm(str, k+1, n);
21             temp = str[i];
22             str[i] = str[k];
23             str[k] = temp;
24         }
25     }
26 }
27 
28 int main()
29 {
30     char str[5] = {"ABCD"};
31     perm(str, 0, 4);
32     return 0;
33 }

 

Guess you like

Origin www.cnblogs.com/chunlinn/p/11265634.html