一维向量旋转算法

《编程珠玑》第二章提到了n元一维向量旋转算法(又称数组循环移位算法)的五种思路,并且比较了它们在时间和空间性能上的区别和优劣。

问题描述

将一个n元一维向量向左旋转i个位置。例如,假设n=8,i=3,向量abcdefgh旋转为向量defghabc。简单的代码使用一个n元的中间向量在n步内可完成该工作。你能否仅使用几十个额外字节的内存空间,在正比于n的时间内完成向量的旋转?

解决方案

思路一:将向量x中的前i个元素复制到一个临时数组中,接着将余下的n-i个元素左移i个位置,然后再将前i个元素从临时数组中复制到x中余下的位置。性能:这种方法使用了i个额外的位置,如果i很大则产生了过大的存储空间的消耗。

C++代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*************************************************************************
> File Name: vector_rotate.cpp
> Author: SongLee
> E-mail: lisong.shine@qq.com
> Created Time: 2014年06月04日 星期三 17时07分06秒
> Personal Blog: http://songlee24.github.io
************************************************************************/

#include<iostream>
#include<string>
using namespace std;

int main()
{

string s = "abcdefghijklmn";
cout << "The origin is: " << s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 将前i个元素临时保存
string tmp(s, 0, i);
// 将剩余的左移i个位置
for(int j=i; j<s.size(); ++j)
{
s[j-i] = s[j];
}
s = s.substr(0, s.size()-i) + tmp;
cout << "The result is: "<< s << endl;
return 0;
}

思路二:定义一个函数将x向左旋转一个位置(其时间正比于n),然后调用该函数i次。性能:这种方法虽然空间复杂度为O(1),但产生了过多的运行时间消耗。

C++代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*************************************************************************
> File Name: vector_rotate_1.cpp
> Author: SongLee
> E-mail: lisong.shine@qq.com
> Created Time: 2014年06月04日 星期三 19时49分59秒
> Personal Blog: http://songlee24.github.io
************************************************************************/

#include<iostream>
#include<string>
using namespace std;

void rotateOnce(string &s)
{

char tmp = s[0];
int i;
for(i=1; i<s.size(); ++i)
{
s[i-1] = s[i];
}
s[i-1] = tmp;
}


int main()
{

string s = "abcdefghijklmn";
cout << "The origin is: " << s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 调用函数i次
while(i--)
{
rotateOnce(s);
}
cout << "The result is: "<< s << endl;
return 0;
}

思路三:移动x[0]到临时变量t中,然后移动x[i]到x[0]中,x[2i]到x[i],依次类推,直到我们又回到x[0]的位置提取元素,此时改为从临时变量t中提取元素,然后结束该过程(当下标大于n时对n取模或者减去n)。如果该过程没有移动全部的元素,就从x[1]开始再次进行移动,总共移动i和n的最大公约数次。性能:这种方法非常精巧,像书中所说的一样堪称巧妙的杂技表演。空间复杂度为O(1),时间复杂度为线性时间,满足问题的性能要求,但还不是最佳。

C++代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*************************************************************************
> File Name: vector_rotate_2.cpp
> Author: SongLee
> E-mail: lisong.shine@qq.com
> Created Time: 2014年06月04日 星期三 20时21分59秒
> Personal Blog: http://songlee24.github.io
************************************************************************/

#include<iostream>
#include<string>
using namespace std;

// 欧几里德(辗转相除)算法求最大公约数
int gcd(int i, int j)
{

while(1)
{
if(i > j)
{
i = i%j;
if(i == 0)
{
return j;
}
}
if(j > i)
{
j = j%i;
if(j == 0)
{
return i;
}
}
}
}

int main()
{

string s = "abcdefghijklmn";
cout << "The origin is: "<< s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 移动
char tmp;
int times = gcd(s.size(), i);
for(int j=0; j<times; ++j)
{
tmp = s[j];
int pre = j; // 记录上一次的位置
while(1)
{
int t = pre+i;
if(t >= s.size())
t = t-s.size();
if(t == j) // 直到tmp原来的位置j为止
break;
s[pre] = s[t];
pre = t;
}
s[pre] = tmp;
}
cout << "The result is: "<< s << endl;
return 0;
}

思路四:旋转向量x实际上就是交换向量ab的两段,得到向量ba,这里a代表x的前i个元素。假设a比b短。将b分割成$b_l$和$b_r$,使$b_r$的长度和a的长度一样。交换a和$b_r$,将$ab_lb_r$转换成$b_rb_la$。因为序列a已在它的最终位置了,所以我们可以集中精力交换b的两个部分了。由于这个新问题和原先的问题是一样的,所以我们以递归的方式进行解决。这种方法可以得到优雅的程序,但是需要巧妙的代码,并且要进行一些思考才能看出它的效率足够高。

1
// 略

思路五:(最佳)将这个问题看做是把数组ab转换成ba,同时假定我们拥有一个函数可以将数组中特定部分的元素逆序。从ab开始,首先对a求逆,得到$a^r b$,然后对b求逆,得到$a^r b^r$。最后整体求逆,得到$(a^r b^r)^r$,也就是ba。
reverse(0, i-1) /cbadefgh/
reverse(i, n-1) /cbahgfed/
reverse(0, n-1) /defghabc/
性能:求逆序的方法在时间和空间上都很高效,而且代码非常简短,很难出错。

C++代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*************************************************************************
> File Name: vector_rotate.cpp
> Author: SongLee
> E-mail: lisong.shine@qq.com
> Created Time: 2014年06月04日 星期三 23时37分54秒
> Personal Blog: http://songlee24.github.io
************************************************************************/

#include<iostream>
#include<string>
using namespace std;

void reverse(string &s, int begin, int end)
{

while(begin < end)
{
char tmp = s[begin];
s[begin] = s[end];
s[end] = tmp;
++begin;
--end;
}
}

int main()
{

string s = "abcdefghijklmn";
cout << "The origin is: "<< s << endl;

int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}

reverse(s, 0, i-1);
reverse(s, i, s.size()-1);
reverse(s, 0, s.size()-1);

cout << "The result is: "<< s << endl;
return 0;
}

扩展延伸

如何将向量abc旋转变成cba?
和前面的问题类似,此向量旋转对应着非相邻内存块的交换模型。解法很相似,即利用恒等式:

$$cba = (a^r b^r c^r)^r$$

:在面试或笔试时,如若出现向量旋转(内存块交换)问题,最好使用思路五答题,不仅高效而且简洁。