Right Shift of array Loops

Source: Internet
Author: User

First, assume that we have an array with 6 elements: 1, 2, 3, 4, 5, 6.

Now we need to shift this array four times to the right, and we can directly launch it with the following results: 3, 4, 5, 6, 1, 2. But how can we solve this problem?

I think the easiest way to think of this is:

Step 1: Save the value of the last element in the array.

Step 2: Move one position from the first element to the last element to the right.

Step 3: place the saved value to the first position of the empty array.

Although this method is simple to remember, it is not very efficient, and its time complexity is O (n ^ 2 ).

Now, I will introduce another algorithm with high efficiency, which makes full use of the features of modulus remainder.

The time complexity of this algorithm is O (n), which is much more efficient than the previous algorithm.

The main implementation steps are as follows: (remember to move the number of moves)

Step 1: Initialize pre_value as the first element of the array.

Step 2: start from a position (initially a is 0) (here, a, B ...... indicates the position marked by the subscript of the array). Copy the pre_value to the Temporary Variable temp, and add the number of times the position a is moved to get a new position.

B = a + move,

Step 3: Save the original data of location B to pre_value, and then copy the value in the temp variable to location B.

Step 4: Repeat Step 2 and Step 3 n (n is the length of the array) to update all elements to the correct position. Then, you can get the correct shifted array.

However, we should note that such a problem may exist.

For example, move the array, three times to the right using the appeal algorithm, and the result is

  

Here we can see that a ring with only two elements is formed, and it does not contain all elements. Therefore, after calculation, we will get special processing in the two cases,

Case 1: the number of shifts is an even number.

Case 2: array length % shift COUNT = 0

The solution is to divide one shift action into multiple shifts without the above two situations. For example, to split one even shift action into n-1 first odd shifts, then perform one shift.

(Note: Pay attention to the second issue)

The following is my implementation code:

  

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 // This function is not applicable when the array length is an even number and the number of shifts is an even number, if the length of the array can be divisible by the number of shifts, you must also consider 5 //. Therefore, for even numbers, we need to break it down into 6 void bufferrightshift (INT buffer [], int N, int count) 7 {8 int I, j; 9 int temp, pre_value; 10 pre_value = buffer [0]; 11 if (COUNT = 0) 12 return; 13 for (I = 0, j = 0; I <n; I ++) 14 {15 temp = pre_value; 16 pre_value = buffer [(j + count) % N]; 17 buffer [(j + count) % N] = temp; 18 J = (J + Count) % N; 19} 20} 21 22 23 void ror (INT buffer [], int N, int count) 24 {25 int move = count % N; 26 if (move % 2 = 0) | (N % Move = 0 )) // The preceding two cases have their own Processing Methods: 27 {28 int K = 1; 29 int I = move; 30 While (1) 31 {32 if (I = 1) 33 break; 34 if (I % 2 = 0) | (N % I = 0 )) // count the number of shifts into 35 {36 K ++; 37 I --; 38} 39 else 40 break; 41} 42 bufferrightshift (buffer, N, move-k); 43 for (I = 1; I <= K; I ++) 44 bufferrightshift (Buffer, N, 1); 45 46} 47 else48 {49 bufferrightshift (buffer, N, move); 50} 51} 52 53 int main () 54 {55 int N, count, i; 56 int * A; 57 while (scanf ("% d", & N, & COUNT )! = EOF) 58 {59 A = (int *) malloc (N * sizeof (INT); 60 for (I = 0; I <n; I ++) 61 scanf ("% d", & A [I]); 62 63 ror (A, N, count); 64 65 for (I = 0; I <n-1; I ++) 66 {67 printf ("% d", a [I]); 68} 69 printf ("% d \ n", a [n-1]); 70 free (void *) A); 71} 72 return 0; 73 74}

Running result:

  

  

Right Shift of array Loops

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.