// Pre-condition: str is a valid C String, and k is non-negative and // less than or equal to the length of str. // Post-condition: All of the permutations of str with the first k // characters fixed in their original positions are // printed. Namely, if n is the length of str, then // (n-k)! permutations are printed. void RecursivePermute(char str[], int k) { int j; // Base-case: Since all letters are fixed, we can ONLY print // what's stored in str. if (k == strlen(str)) printf("%s\n", str); else { // Loop through each possible starting letter for index k, // the first index for which we have a choice. for (j=k; j<strlen(str); j++) { // Place the character stored in index j in location k. ExchangeCharacters(str, k, j); // Print out all of the permutations with that character // just chosen above fixed. RecursivePermute(str, k+1); // Put the original character that used to be there back // in its place. ExchangeCharacters(str, j, k); } } }
// Pre-condition: 0 < n < 8 (upper limit due to lots of printing) // Post-condition: All permutations of 0,1,...,n-1 will // be printed. void ListPermutations(int n) { // Set up used and permutation arrays. int* used = calloc(n,sizeof(int)); int* perm = calloc(n,sizeof(int)); RecursivePermute(n, 0, perm, used); free(used); free(perm); }
// Pre-condition: str is a valid C String. // Post-condition: All permutations of str (assuming all distinct // characters) will be printed. void ListPermutations(char str[]) { // Call the appropriate recursive function with the correct // parameters. int n = strlen(str); int* used = calloc(n,sizeof(int)); int* perm = calloc(n,sizeof(int)); RecursivePermute(str, 0, perm, used); free(used); free(perm); }
static void RecursivePermute(string str, int k, setADT set) { int i; if (k == StringLength(str)) { AddPtrElement(set, CopyString(str)); } else { for (i = k; i < StringLength(str); i++) { ExchangeCharacters(str, k, i); RecursivePermute(str, k + 1, set); ExchangeCharacters(str, k, i); } } }
static void ListPermutations(string str) { setADT set; iteratorADT iterator; string s; set = NewPtrSet(StringCmpFn); RecursivePermute(str, 0, set); iterator = NewIterator(set); while (StepIterator(iterator, &s)) { printf("%s\n", s); } FreeIterator(iterator); }
// Prints out all permutations of str where the first k // characters are str[perm[0]], str[perm[1]]...str[perm[k-1]] // where perm stores the permutation to be printed and used // stores whether or not each item belongs in perm[0]...perm[k-1]. void RecursivePermute(char str[], int k, int* perm, int* used) { int i; // We've filled perm already. Print the corresponding permutation. if (k == strlen(str)) { for (i=0; i<strlen(str); i++) printf("%c", str[perm[i]]); printf("\n"); } // Try each new item in spot k. for (i=0; i<strlen(str); i++) { if (!used[i]) { perm[k] = i; used[i] = 1; RecursivePermute(str, k+1, perm, used); used[i] = 0; } } }
// Prints out all permutations of 0,1,...,n-1 with the first // k slots stored in perm and used[i] = 1 if i is in // perm[0..k-1] and 0 otherwise. void RecursivePermute(int n, int k, int* perm, int* used) { int i; // We've filled perm already. Print the corresponding permutation. if (k == n) { for (i=0; i<n; i++) printf("%d ", perm[i]); printf("\n"); } // Try each unused number in spot k. for (i=0; i<n; i++) { if (!used[i]) { perm[k] = i; used[i] = 1; RecursivePermute(n, k+1, perm, used); used[i] = 0; } } }
// Pre-condition: str is a valid C String. // Post-condition: All permutations of str (assuming all distinct // characters) will be printed. void ListPermutations(char str[]) { // Call the appropriate recursive function with the correct // parameters. RecursivePermute(str, 0); }