2楼 迭代算法分析 - Fischer-Krause ordered permutation 转 C 代码,以及解释
[4楼]递归+元素调换 —— 最直接的算法
[5楼]耳目一新的迭代法 —— 通过进制转换原理计算模式,再应用到排列
[算法]Permutation - 元素排列
- 523066680
- Administrator
- 帖子: 542
- 注册时间: 2016年07月19日 12:14
- 联系:
Fischer-Krause ordered permutation 转 C 代码,以及解释
Fischer-Krause ordered
Here's a little program that generates all permutations of all the words
on each line of input. The algorithm embodied in the "permute()"
function is discussed in Volume 4 (still unpublished) of Knuth's *The
Art of Computer Programming* and will work on any list:
来自 perldoc -q permute, 调用示例:echo a b c | permute.pl
解读和代码转C,主要是有一个数字调换的规律
有了这一个规则,我们可以 通过某个中间的排列得出下一个结果:
举一个 6 位的
arr[] = 5 3 4 2 1 0
等有时间就做个图什么的... 未完待续
Here's a little program that generates all permutations of all the words
on each line of input. The algorithm embodied in the "permute()"
function is discussed in Volume 4 (still unpublished) of Knuth's *The
Art of Computer Programming* and will work on any list:
Code: [show] | [select all]
解读和代码转C,主要是有一个数字调换的规律
Code: [show] | [select all]
举一个 6 位的
arr[] = 5 3 4 2 1 0
- 从右往左寻找 前一项小于当前项 的情况(3和4), 下标 p = 2; 记住前一项 = 3
- 反转以 p 为起点,至末尾的项, arr[] = 5 3 0 1 2 4
- 之后的计算依据 5 3 0 1 2 4
- 以 arr[2] 为起点,向右寻找 > 3 的项的下标, 4 > 3, 所以 q = 5;
从 5 3 0 1 2 4 调换 arr[p-1], arr[q], 得
等有时间就做个图什么的... 未完待续
- 523066680
- Administrator
- 帖子: 542
- 注册时间: 2016年07月19日 12:14
- 联系:
[算法]Permutation - 元素排列,递归方案
另一段 Perl 排列元素的代码 看上去相当简练,不过应该还是递归提取和堆叠元素的思路,只是语法糖用的比较到位。
Permutations using Perl
Permutations using Perl
steabert - There is a nice lecture on YouTube in the Stanford programming paradigms series about doing permutation with recursion and double mapping in Scheme. In Perl, I came up with the following implementation for the algorithm:
Code: [show] | [select all]
- 523066680
- Administrator
- 帖子: 542
- 注册时间: 2016年07月19日 12:14
- 联系:
C,递归,目前看到的最直接也最简练的算法
这段代码引自:http://bbs.bathome.net/thread-43749-1-1.html
和常规的递归思路一样,层层提取,但是把递归结构应用到极致,没有多余的副本创建,整个代码展现出一种概念:
Code: [show] | [select all]
- 数据 即 结构,结构 即 数据。
- 523066680
- Administrator
- 帖子: 542
- 注册时间: 2016年07月19日 12:14
- 联系:
耳目一新的迭代法 —— 通过进制转换原理计算模式,再应用到排列
编辑/整理:523066680@163.com
日期:2017-05
转载请注明出处:http://www.code-by.org/viewtopic.php?f=29&t=261
序
日期:2017-05
转载请注明出处:http://www.code-by.org/viewtopic.php?f=29&t=261
序
- 通过迭代方式获得排列的方案,参考自《Higher-Order Perl》
- 假设有4个元素: @arr = (a, b, c, d),下标为 0 1 2 3,每提取一个元素,
数组重新定义,下标从0开始。排列和下标的提取关系:
a b c d -> 0 0 0 0
a b d b -> 0 0 1 0
a c b d -> 0 1 0 0
a c d b -> 0 1 1 0
a d b c -> 0 2 0 0
...
注意这里数字的变化和进制换算、递增非常相似,区别在于,每一位的进制是不同的:
末位始终为0,
倒数第二位最大为1(0,1),
倒数第三位最大为2(0,1,2),
倒数第四位最大为3(0,1,2,3)
一共能产生 4×3×2×1 种模式(pattern) (刚好对应24种排列情况)
- 先设计一种换算函数,对于传入的任意数字,计算出对应的模式:
输出: 0,2,1,0Code: [show] | [select all]
- 再写一个函数将 模式应用于顺序地提取数组元素
输出:a,d,c,bCode: [show] | [select all]
这样,不管想要哪一个序列,只要通过类似进制换算的方法算出模式,按模式提取即可
- Code: [show] | [select all]
- rubyish
- 渐入佳境
- 帖子: 52
- 注册时间: 2018年04月23日 09:58
- 联系:
- 523066680
- Administrator
- 帖子: 542
- 注册时间: 2016年07月19日 12:14
- 联系:
Re: [算法]Permutation - 元素排列
我就想起来你在 CU 的蜜汁代码rubyish 写了:laige perl ~~
Code: [show] | [select all]
代码: 全选
E_( [ 1 .. 9 ], [] );
sub E_
{
my ( $a, $b ) = @_;
#此处省略若干代码
E_( [ @$a[ 0 .. $_ - 1, $_ + 1 .. $#$a ] ], [ @$b, $a->[$_] ] )
for 0 .. $#$a;
}
您没有权限查看这个主题的附件。
- rubyish
- 渐入佳境
- 帖子: 52
- 注册时间: 2018年04月23日 09:58
- 联系:
- rubyish
- 渐入佳境
- 帖子: 52
- 注册时间: 2018年04月23日 09:58
- 联系:
在线用户
正浏览此版面之用户: 没有注册用户 和 1 访客