Normal and cyclic shifts of arrays in C ++

Source: Internet
Author: User

Normal shift:

If you want to shift n digits from the last position of an array to the right, the first position of the array is shifted to the right.

The procedure is as follows:

View code

# Include <iostream>
# Include <stdlib. h>
Using namespace STD;
Int s [11] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
Void main ()
{
For (INT I = 0; I <11; I ++)
{
Cout <s [I];
}
Cout <Endl;
// Shift code, which is assumed to shift to the right
For (Int J = 10; j> 0; j --)
{
S [J] = s [J-1];
}
For (int K = 0; k <11; k ++)
{
Cout <s [k];
}
Cout <Endl;
}

Program running result:

  

Cyclic shift:

View code

# Include <iostream>
# Include <stdlib. h>
Using namespace STD;
Int s [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
Void main ()
{
Int temp;
// Shifts three places to the right of the loop
For (INT I = 1; I <= 3; I ++)
{
Temp = s [9];
For (Int J = 9; j> = 1; j --)
{
S [J] = s [J-1];
}
S [0] = temp;
}
For (int K = 0; k <= 9; k ++)
{
Cout <s [k];
}
}

In fact, the principle is to shift the array through an indirect variable. Assume that the array length is n and the number of shifts to the right needs to be shifted K times. The complexity of the algorithm is O (K * n ), when K> N, the algorithm complexity is even greater than O (N ^ 2 ).

Running result diagram:

Through analysis, we can find that K> N is the same as k' = K % N, and the following algorithm is derived:

View code

# Include <iostream>
# Include <stdlib. h>
Using namespace STD;
Int s [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
Void main ()
{
Int temp;
Int K, KK;
K = 99;
// Shift K bits to the right
Kk = K % 10; // The array length is 10
For (INT I = 1; I <= K; I ++)
{
Temp = s [9];
For (Int J = 9; j> = 1; j --)
{
S [J] = s [J-1];
}
S [0] = temp;
}
For (int K = 0; k <= 9; k ++)
{
Cout <s [k];
}
Cout <Endl;
}

The running result is omitted.

Find an O (n) method on the Internet. The principle is as follows:

Assume that the original order column is abcd1234.

After the four-digit shift, the value is 1234 ABCD.

The shift process can be expressed as follows:

Sort ABCD in reverse order: dcba1234

Sort in reverse order 1234: dcba4321

Forward dcba4321: 1234 ABCD

That is to say, you need to write a reverse function and perform a cubic reverse transformation.

The Code is as follows:

View code

1 # include <iostream>
2 # include <stdlib. h>
3 using namespace STD;
4 void reverse (int * arr, int B, int e );
5 Int s [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
6 void main ()
7 {
8 int K, KK, N;
9 N = 10;
10 K = 99;
11 KK = K % 10;
12 // The following is the cyclic shift part.
13 reverse (S, 0, N-kk-1 );
14 reverse (S, N-KK, N-1 );
15 reverse (S, 0, N-1 );
16 // display
17 For (INT I = 0; I <= 9; I ++)
18 {
19 cout <s [I];
20}
21 cout <Endl;
22}
23 void reverse (int * arr, int B, int e)
24 {
25 int temp;
26 For (; B <E; B ++, e --)
27 {
28 temp = arr [E];
29 arr [e] = arr [B];
30 arr [B] = temp;
31}
32}

 

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.