For arrays with n elements int A [n] = {...}; write an efficient Algorithm Shifts the array content to the left by repeating
For example, int A [6] = {1, 2, 3, 4, 5, 6}. After the loop is shifted three places to the left, {456123} is returned },
Requirements:
1. You are not allowed to apply for an array space, but you can apply for a few variables.
2 left shift is not allowed each time
//////////////////////////////////////// //////
The basic idea of an algorithm is as follows:
There is an array containing 8 numbers: int A [8] = {1, 2, 3, 4, 5, 6, 7, 8}; shifts the content of the array to the left by repeating, that is,
The result is 4, 5, 6, 7, 8, 1, 2, 3.
From the above example, we can see that we moved 1, 2, 3 to the end of the array, starting from 4, until 8 all moved forward to three positions. So we can imagine
Consider 1, 2, 3 as a whole and exchange with 4, 5, 6, and then 7, 8.
Result of the first exchange:
4, 5, 6, 1, 2, 3, 7, 8
Because there are only two digits in 7, 8, the second exchange is only exchanged with 1, 2, and 3. The result is as follows:
4, 5, 6, 7, 8, 3, 1, 2.
It can be seen that the first five digits have met the final result, and the last three digits have not yet met the final result. However, you only need to consider 3, 1, 2 as
Sub-array, and then move the sub-array to the left to change to 1, 2, 3.
Therefore, when moving an element, the M bit to be left shifted can be regarded as a whole (1, 2, 3 in the above example), which is in turn the same as
Array element exchange. If the following elements are less than m bits, assume t bit, T <m, then only T bit is exchanged. In this way, array elements (the first N-M elements must meet the conditions ).
Finally, let's look at the next M elements into a sub-array, and then shift them left by M-N % m.
The time complexity of the algorithm is O (n), and the space complexity is O (1 ).
C implementation is as follows (move_array is an algorithm implementation function with a test case ):
# Include < Stdio. h >
Void Swap ( Int * A, Int * B)
{
Int X;
X = * A;
* A = * B;
* B = X;
}
// This function is used for algorithm implementation.
/////////////////////////////////////// //
Void Move_array ( Int Data [], Int N, Int M)
{
Int I, J;
M = M % N;
If (M = 0 ) Return ;
For (I = 0 ; I < N - M; I + = M)
{
For (J = I; j < I + M && J < N - M; j ++ )
{
Swap ( & Data [J], & Data [J + M]);
}
}
Move_array (Data + N - M, M, m - N % M );
}
/////////////////////////////////////// //
Int Main ()
{
Int Data1 [] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
Int Data2 [] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 };
Int Data3 [] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
Int Data4 [] = { 1 , 2 , 3 , 4 , 5 , 6 };
Int Data5 [] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 };
Int I;
Move_array (data1, 8 , 5 );
For (I = 0 ; I < 8 ; I ++ )
Printf ( " % D " , Data1 [I]);
Printf ( " \ N " );
Move_array (data2, 14 , 18 );
For (I = 0 ; I < 14 ; I ++ )
Printf ( " % D " , Data2 [I]);
Printf ( " \ N " );
Move_array (data3, 9 , 3 );
For (I = 0 ; I < 9 ; I ++ )
Printf ( " % D " , Data3 [I]);
Printf ( " \ N " );
Move_array (data4, 6 , 3 );
For (I = 0 ; I < 6 ; I ++ )
Printf ( " % D " , Data4 [I]);
Printf ( " \ N " );
Move_array (data5, 18 , 17 );
For (I = 0 ; I < 18 ; I ++ )
Printf ( " % D " , Data5 [I]);
Printf ( " \ N " );
Return 0;
}