Notes on programming Pearl River (version 2nd) -- rotate an N-element vector to the left and I position (chapter 2)

Source: Internet
Author: User
Tags modulus
Chapter 2 Aha! Algorithm

Chapter 2nd of programming Pearl River (version 2nd) gave three questions at the beginning. Question B is very interesting: rotating an N-yuan vector to the left to I position. For example, when n = 8 and I = 3, the vector abcdefgh is rotated to defghabc. Simple code uses an N-element intermediate vector to complete this work in n steps. Can you use only dozens of extra bytes of storage space, it is proportional to the time of N to complete vector rotation.

In fact, as mentioned in the question, if you do not consider space, this is a very simple problem. But can I solve this problem smartly when considering the space?

The book provides two sophisticated methods:

1. Method of modulus replacement:

In fact, we know that there is a space-saving solution: each time you rotate a position to the left (the time is proportional to N), you need to rotate for a total of I times. 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. Take the following two examples:

Example array: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

(1) n = 12 and I = 5:

(2) n = 12 and I = 3:

The above two figures show two different situations. The maximum common divisor of n = 12 and I = 5 in example (1) is 1, so the constant modulo can traverse the array without repeating; in Example (2), n = 12 and I = 3 have the maximum number of public approx. 3, so the three positions are moved to the same place, therefore, you need to move to the next position to evaluate the modulus three times in total:

// Calculate the approximate number of unsigned int gcd (unsigned int A, unsigned int B) {unsigned int temp; while (B! = 0) {temp = A % B; A = B; B = temp;} return a;} void modshift (INT array [], int N, int rotdist) {unsigned int GCD = gcd (n, rotdist); For (INT I = 0; I <GCD; I ++) {int temp = array [I]; int J = I; int K; while (1) {int K = J + rotdist; If (k> = N) {k-= N ;} if (k = I) {break;} array [J] = array [k]; j = K;} array [J] = temp ;}}

2. Methods of piecewise recursive switching:

In fact, the rotating vector x is the two blocks of the exchange vector AB to obtain Ba (A represents the first I element in X ). 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. The Code is as follows:

//swapShift//swap x[a .. a+offset-1] and x[b .. b+offset-1]void swap(int array[], int a, int b, int offset){int temp;for (int i = 0; i < offset; i++){temp = array[a + i];array[a + i] = array[b + i];array[b + i] = temp;}}void swapShift(int *array, int n, int rotdist){int p = rotdist;int i = p;int j = n - p;while (i != j){if (i > j){swap(array, p - i, p, j);i -=j;}else{swap(array, p - i, p + j - i, i);j -= i;}}swap(array, p - i, p, i);}

3. Inverse Method (FLIP algorithm)

Using Vector principle: divide the X vector into two parts: AB, a is the first I element, B is the last n-I element, first obtain the inverse of a, get the a-1b, then calculate the inverse of B to obtain the a-1b-1, and then obtain the inverse of the whole (a-1b-1)-1 = BA.

The Code is as follows:

//Reversevoid reverse(int array[], int low, int high){int temp = 0;for(int i = low; i <= (high + low) / 2; i++){temp = array[i];array[i] = array[high - (i - low)];array[high - (i - low)] = temp;}}void reverseShift(int *array, int n, int rotdist){reverse(array, 0, rotdist - 1);reverse(array, rotdist, n - 1);reverse(array, 0, n - 1);}

The code of the flip algorithm is very short and easy to understand. In addition, you do not need to write functions for string inversion, which is efficient in both time and space.

Summary:

A simple question is that different methods can be found from different perspectives, mainly through divergent thinking and efficient and easy-to-understand methods.

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.