完美洗牌算法
2012-11-21 00:00
211 查看
暂时没有理解清楚,主要是个置换环,但是构造的算法,感觉很牛逼
先贴上代码,留个纪念:
先贴上代码,留个纪念:
/*=============================================*\ 完美洗牌算法: 有个长度为2n的数组{a1,a2,a3,...,an,b1,b2,b3,...,bn}, 希望排序后{a1,b1,a2,b2,....,an,bn}, 要求时间复杂度o(n),空间复杂度0(1)。 \*=============================================*/ #include <iostream> #include <string> using namespace std; int main(){ string arr("1234567abcdefg"); int index = arr.length() / 2; int temp = arr[index]; while(index != 1){ int tempIndex = (index + (index % 2) * (arr.length() - 1)) / 2; arr[index] = arr[tempIndex]; index = tempIndex; } arr[1] = temp; cout << arr << endl; system("pause"); }
相关文章推荐
- 完美洗牌算法(1)
- 完美洗牌算法学习
- 完美洗牌算法学习
- 关于完美洗牌算法中圈和圈起点的一个证明
- (算法)完美洗牌
- 完美洗牌算法学习
- 格子取数问题,完美洗牌算法
- 关于完美洗牌算法中圈和圈起点的一个证明
- 完美洗牌算法(2013年UC校招笔试、2016阿里实习生笔试)
- 算法笔记_128:完美洗牌算法(Java)
- 对锦标赛算法与完美洗牌的分析
- 算法---完美洗牌(难)
- 完美洗牌算法
- 【★】SPF(Dijkstra)算法完美教程
- [弦图 最小染色 完美消除序列 MCS算法] BZOJ 1006 [HNOI2008]神奇的国度
- 12、Java入门—将一数组乱序排列的三种方法 (快速洗牌的小算法)
- 关于完美洗牌问题的若干思考(扩展)
- 二分图的最大匹配、完美匹配和匈牙利算法
- 完美洗牌
- uva 10710 - Chinese Shuffle(完美洗牌)