The algorithm teacher gave a hard-pressed proof of the full arrangement. The following is an example:
Analyze the correctness and time efficiency of the following generation algorithm: (for convenience of testing, the source code is appended to the end)
Heappermute(N)
// Generate and arrangeHeapAlgorithm
// Input: a positive integerNAnd a Global ArrayA[1 ..N]
// Output:AFull arrangement of elements in
IfN= 1
WriteA
Else
ForI
Limit 1To N Do
Heappermute(N-1)
If
NIs odd
SwapA[1] andA[N]
Else
SwapA[I] andA[N]
Here is my proof:
When n = 1, the output sequence is 1.
When n = 2, the output sequence is 1 2; 2 1, true.
Assume that when n = 2 K, the output is a fully arranged sequence, and the values in the array are shifted to the right after heappermute (n) is passed. When n = 2 k + 1, the output is a fully arranged sequence, and the values in the array remain unchanged after heappermute (n) is passed.
So when n = 2 K + 2:
Since n = 2 K + 2 is an even number, when n enters the heappermute method, heappermute (2 k + 1) is first executed. We can see from the assumption that for an odd number of 2 k + 1, after heappermute (2 k + 1) is created, the value position in the array does not change, and the first 2 k + 1 is worth sorting. Therefore, swap (A [I], a [n]) the statement causes each value in the original array to be at the 2n + 2 position. For heappermute (2 k + 1), each process is a full arrangement of the 2 k + 1 number, therefore, you can obtain heappermute (2 k + 2) and get a full arrangement of 2 K + 2 numbers.
After N cycles, we can see the original 1, 2 ,.... 2 k + 1 sequentially moved a position, the first position is occupied by 2 K + 2, so when n = 2 K + 2 is an even number, the elements in the array are shifted to the right by one.
When N = 2 K + 3:
Since n = 2 K + 3 is an odd number, when n enters the heappermute method, heappermute (2 k + 2) is first executed. We can see from the assumption that for an even number of 2 K + 2, after the heappermute (2 k + 2) is created, the value position in the array shifts to the right of the array, and the first 2 K + 2 elements are arranged in full order. Therefore, we can ensure that in the whole process of for I <-1 to n, swap (A [1], a [n]) puts a [1 .. n] data is placed in the last position a [n], and then the first 2 K + 2 elements are arranged in full, heappermute (2 k + 2 ). Therefore, after heappermute (2 k + 3), the output is a full arrangement of 2 K + 3 elements. After the process is met, the order of elements in the array remains unchanged.
What is the internal mechanism of this method?
First, we can see
Source code -------------------
# Define N 4
# Include <stdio. h>
# Include <stdlib. h>
Int A [9] = {1, 2, 4, 5, 6, 7, 8, 9 };
File * out;
Inline swap (Int & A, Int & B)
{
Int temp;
Temp =;
A = B;
B = temp;
}
Void print ()
{
Int J = 0;
For (j = 0; j <n; j ++)
Fprintf (Out, "% d", a [J]);
Fprintf (Out, "\ n ");
}
Void heappermute (int n)
{
Int I = 0;
If (n = 1)
Print ();
Else
{
For (I = 0; I <n; I ++) // the odd values do not change the order, and the even values move the cycle left.
{
Heappermute (n-1 );
If (N % 2)
Swap (A [0], a [n-1]);
Else
Swap (A [I], a [n-1]);
}
}
}
Void main ()
{
Out = fopen ("jieguo.txt", "W ");
Heappermute (N );
Fprintf (Out ,"--------------------------------");
Print ();
Exit (1 );
}