Rotation: Array Loop shift

Source: Internet
Author: User

Array Loop shift

Requirements: design an algorithm that shifts an array of N elements to the right of K bits, requiring a time complexity of O(N) and allowing only two additional variables to be used.

The test instructions solution is as follows:

Let's experiment with the simple method of moving the elements in the array to the right one bit at a time, looping K times. ABCD1234→4abcd123→34abcd12→234abcd1→1234ABCD.

Version 1

void RightShift (char *arr, int N, int k)
{
while (k--)
{
Char t = arr[n-1];
for (int i = N-1; i > 0; i--)
Arr[i] = arr[i-1];
Arr[0] = t;
}
}


Although this algorithm can realize the loop right shift of the array, but the algorithm complexity is O(K * N), does not meet the requirements of the topic, need to continue to explore.


Analysis and Solution

If the array is ABCD1234 and the loop is shifted right by 4 bits, we want to reach the state of 1234ABCD. It is advisable to set K as a non-negative integer, and when K is a negative integer, move the K -bit to the right, which is equivalent to the left shift (-K) bit. The left and right shifts are essentially the same.

"Solution One"

There may be a potential hypothesis,K<N, at first. In fact, many times it is true. But strictly speaking, we can not use such "inertia thinking" to think about the problem. Especially when programming, it is important to consider the problem comprehensively,K may be an integer greater than N , at this time, the above solution needs to be improved.

It is easy to observe the characteristics of the loop right shift, and it is not difficult to find out that each element will return to its own position after the N -bit is shifted right. So, if k > n, the array sequence after the right shift K-n is the same as the result of the right shift K -bit. In turn, a general rule is drawn: The case after the right shift of K -bit is the same as after the right-shift k' = k - N -bit.

Version 2

void RightShift (char *arr, int N, int k)
{
K%= N;
while (k--)
{
Char t = arr[n-1];
for (int i = N-1; i > 0; i--)
Arr[i] = arr[i-1];
Arr[0] = t;
}
}


It can be seen that the complexity of the algorithm is reduced to O(N2) After considering the characteristics of the cyclic right shift, which is not related to K , and the requirement of the topic is close to one step. But the complexity of the time is not low enough, so let's continue digging around the link between the loop right shift and the array.

"Solution Two"

Assuming that the original array sequence is ABCD1234, the sequence of arrays required to be transformed is 1234ABCD, that is, the loop is shifted right by 4 bits. After comparison, it is not difficult to see that there are two paragraphs of the order is unchanged: 1234 and ABCD, can be regarded as two of the two paragraphs. The process of moving the K -bit right is to swap the two parts of the array. The transformation process is accomplished by the following steps:

1. Reverse order ABCD:abcd1234→ DCBA1234;

2. Reverse order 1234:DCBA1234→ dcba4321;

3. All reverse:DCBA4321→1234abcd.

Version 3

Reverse (char *arr, int b, int e)
{
for (; b < e; b++, e--)
{
Char temp = arr[e];
Arr[e] = arr[b];
ARR[B] = temp;
}
}

RightShift (char *arr, int N, int k)
{
K%= N;
Reverse (arr, 0, n-k-1);
Reverse (arr, n-k, N-1);
Reverse (arr, 0, N-1);
}


In this way, we can implement the right shift within linear time.

Rotation: Array Loop shift

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.