One-dimensional vector Rotation Algorithm Programming

Source: Internet
Author: User

After reading the second chapter of programming, I have discussed three questions. Here I will talk about the second question, one-dimensional vector rotation algorithm.

Question: rotate an N-element-dimension vector (for example, an array) to the left of the I position.

Solution: the five methods are described in the book. You can only think of the two simplest methods (the first two methods described below ).

1. original method.

Move one digit from left to right to translate all data. In this way, the complexity of the algorithm reaches n ^ 2 in the case of loop I. time consumption is not recommended.

2. Change the space time.

As the name suggests, apply for an I-length space, put the first half of the space in the application space, and then move all the data in the back to the left of the I-position, finally, put the data in the applied space in the second half. It is a waste of space and is not recommended.

3. Acrobatics algorithm (called by Abacus, also called the modulo replacement method)

We know that the first method is to rotate a position to the left each time (the time is proportional to N), which requires a total of I rotation. This solution consumes too much running time. The method of Modulo replacement is to try to move each number at a time. The general idea is to evaluate the model of N using I as the divisor, traverse the vector and move it in place at a time.

Another programming example: Move X [0] to the temporary variable t, then move X [I] to X [0], X [2I] to X [I], and so on, until X [0] is obtained (where the lower mark is used to modulo the length N), and then X [1] in turn... X [I-1] performs the preceding operation.

This algorithm is very skillful. It is generally difficult to think about it, and the code is slightly complicated and not recommended.

For the code reference, the C ++ format is as follows:

1 // code for calculating the common approx. Euclidean algorithm 2 unsigned int gcd (unsigned int A, unsigned int B) 3 {4 unsigned int temp; 5 while (B! = 0) 6 {7 temp = A % B; 8 A = B; 9 B = temp; 10} 11 12 return; 13} 14 // rotate the array [N] To the left rotdisk position 15 void zcxshift (INT array [], int N, int rotdist) 16 {17 unsigned int GCD = gcd (n, rotdist); 18 19 for (INT I = 0; I <GCD; I ++) 20 {21 int temp = array [I]; 22 Int J = I; 23 int K; 24 while (1) 25 {26 int K = J + rotdist; 27 if (k> = N) 28 {29 K-= N; 30} 31 32 If (k = I) 33 {34 break; 35} 36 37 array [J] = array [k]; 38 J = K; 39} 40 array [J] = temp; 41} 42 43}
View code

4. piecewise recursive Switching Algorithm

The book introduces: the rotating vector X is actually two sections of the exchange vector AB, and Ba (A represents the first I element in X) is obtained ). If a is shorter than B, B is divided into two segments B1 and B2, so that B2 has the same length as A, and then a and b2 are exchanged, that is, ab1b2, to obtain b2b1a, the position of a is already the final position. Now the problem is concentrated on the exchange of b2b1, and the original problem is returned. Recursion continues, and the length of B1 and B2 is equal.

Reference code is provided for the post-book answer. The C ++ description is as follows:

1 // switch operation, as shown below 2 // swap X [.. A + offset-1] and X [B .. B + offset-1] 3 void swap (INT array [], int A, int B, int offset) 4 {5 Int temp; 6 for (INT I = 0; I <OFFSET; I ++) 7 {8 temp = array [A + I]; 9 array [A + I] = array [B + I]; 10 array [B + I] = temp; 11} 12} 13 14 // swap main code 15 void swapshift (int * array, int N, int rotdist) 16 {17 int P = rotdist; 18 int I = P; 19 Int J = N-P; 20 21 While (I! = J) 22 {23 if (I> J) 24 {25 swap (array, p-I, P, J); 26 I-= J; 27} 28 else29 {30 swap (array, p-I, P + J-I, I); 31 J-= I; 32} 33} 34 swap (array, p-I, P, I); 35}
View code

5. Turning method (also called inverse method) [recommendation algorithm]

The idea is very simple. We divide the X vector into two parts: AB. A is the first I element and B is the last n-I element. First, we need to reverse a and then B, then obtain BA from the inverse of the whole.

Similarly, the C ++ code is as follows:

1 // inverse function 2 void reverse (INT array [], int low, int high) 3 {4 int temp = 0; 5 for (INT I = low; I <= (high + low)/2; I ++) 6 {7 temp = array [I]; 8 array [I] = array [High-(I-low)]; 9 array [High-(I-low)] = temp; 10} 11} 12 // code 13 void reverseshift (int * array, int N, int rotdist) 14 {15 reverse (array, 0, rotdist-1 ); 16 reverse (array, rotdist, n-1); 17 reverse (array, 0, n-1); 18}
View code

Algorithm 5 is simple and clear. The code of the flip algorithm is very short and easy to understand. In addition, you do not need to write your own function for string inversion, which is efficient in both time and space. Recommendation algorithm 5 again.

 

Next to the fifth question in the book, flip the AC in the ABC vector, with the same idea as 5. I will not repeat it here!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.