|
p[0] = 0
p[1] = 1
p[2] = 2
...
p[N] = N
Example_03 - Permutation Reversals on the Head of a Linear Array Without Using Recursion
// NOTICE: Copyright 1991-2008, Phillip Paul Fuchs
#define N 12
// number of elements to permute. Let N > 2void example_03(void) { unsigned int a[N], p[N+1];
// target array and index control arrayregister unsigned int i, j, tmp;
// index variables and tmp for swapfor(i = 0; i < N; i++)
// initialize arrays; a[N] can be any type{ a[i] = i + 1;
// a[i] value is not revealed and can be arbitraryp[i] = i; } p[N] = N;
// p[N] > 0 controls iteration and the index boundary for i //display(a); // remove comment to display array a[]i = 1;
// setup first swap points to be 1 and 0 respectively (i & j)while(i < N) { p[i]--;
// decrease index "weight" for i by onej = 0;
// reset j to 0do
// reverse target array from j to i{ tmp = a[j];
// swap(a[i], a[j])a[j] = a[i]; a[i] = tmp; j++;
// increment j by 1i--;
// decrement i by 1} while (j < i);
//display(a); // remove comment to display target array a[]i = 1;
// reset index i to 1 (assumed)while (!p[i])
// while (p[i] == 0){ p[i] = i;
// reset p[i] zero valuei++;
// set new index value for i (increase by one)}
// while(!p[i])}
// while(i < N)}
// example_03()
EXAMPLE 03a - Function to Display Permutations (Optional)
// NOTICE: Copyright 1991-2008, Phillip Paul Fuchs
void display(unsigned int *a) { for(unsigned int x = 0; x < N; x++) printf("%d ",a[x]); printf("\n"); getch();
// press any key to continue...}
// display()
Fundamental Analysis of Permutation Example 03 Above
Number of Objects
to Permute (N)a[N] Display of
Initial Sequencea[N] Display of
Final SequenceTotal Number of Unique
Combinations Produced (N!)3 1 2 3 3 2 1 2 * 3 = 6 4 1 2 3 4 4 3 2 1 6 * 4 = 24 5 1 2 3 4 5 5 4 3 2 1 24 * 5 = 120 6 1 2 3 4 5 6 6 5 4 3 2 1 120 * 6 = 720 7 1 2 3 4 5 6 7 7 6 5 4 3 2 1 720 * 7 = 5040 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 5040 * 8 = 40320 9 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 40320 * 9 = 362880 N 1 2 3 . . . (N - 1) N N (N - 1) . . . 3 2 1 (N - 1)! * N = N!
Often duplicate generations within a complete permutation set are not desirable. If there are duplicate elements within a target array, there will be duplicate generations within the permutation set. The QuickPerm Reversal Algorithm presented above stemmed from an early single-pass Palindrome Detection Algorithm I developed in the year 1984. Depending upon the upper and lower indices, duplicate generations - as a fractional palindrome substring - can be detected and omitted when target elements are swapped. Consider two fundamental target strings such as "ABCDEFFEDCBA" and "ABCDEFABCDEF" to research duplicate patterns within ordered permutation sets. (Please note, the examination of target arrays violates a previously stated guideline: "The linear permutable target can NOT be examined".) Presented below is a "Permutation Ruler" which illustrates this eloquent principle:
A Permutation Ruler: (The Origin)
Next is EXAMPLE 04 which inclusively reverses elements from index i to index j. (Tail Reversals)