(go) array loop right shift

Source: Internet
Author: User

An algorithm is designed to move an array of N elements to the right by the K-bit, 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. The pseudo code is as follows:

Code Listing 2-33

RightShift (int* arr, int N, int K)

{

while (k--)

{

int 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 to the right 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, equivalent to the left shift (-K). The left and right shifts are essentially the same.

"Solution One"

There may be a potential hypothesis, k<n. 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.

Code Listing 2-34

RightShift (int* arr, int N, int K)

{

K%= N;

while (k--)

{

int 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 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 the array required to be transformed is 1234ABCD, that is, the loop shifts 4 bits to the right. 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. abcd:abcd1234→dcba1234 in reverse order;

2.1234:dcba1234→dcba4321 in reverse order;

3. All reverse order: DCBA4321→1234ABCD.

Pseudo-code can be referenced as follows:

Code Listing 2-35

Reverse (int* arr, int b, int e)

{

for (; b < e; b++, e--)

{

int temp = Arr[e];

Arr[e] = arr[b];

ARR[B] = temp;

}

}

RightShift (int* 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.

(go) array loop right shift

Related Article

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.