如何在算法数组的递归打印排列的工作是什么呢? [英] How does the algorithm for recursively printing permutations of an array work exactly?

查看:131
本文介绍了如何在算法数组的递归打印排列的工作是什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是无法理解这种算法的工作。所有我见过的解释说,如果你有一组如{A,B,C},你想所有的排列,先从每个字母清楚,然后找到信件的其余部分的排列。因此,例如{A} + permutationsOf({B,C})。

I just can't understand how this algorithm works. All the explanations I've seen say that if you have a set such as {A, B, C} and you want all the permutations, start with each letter distinctly, then find the permutations of the rest of the letters. So for example {A} + permutationsOf({B,C}).

不过,所有的解释似乎掩盖你如何的找到其余的排列的。一个例子是这个

But all the explanations seem to gloss over how you find the permutations of the rest. An example being this one.

可能有人试图解释清楚这个算法多一点给我吗?

Could someone try to explain this algorithm a little more clearly to me?

推荐答案

时,您需要了解递归理解递归..

(三)程序员的智慧

您的问题是关于这一事实,即剩下的置换就是递归部分。递归总是由两部分组成:简单的情况和递归情况。小例子指出了一个案例时,有没有继续递归和东西应该归还。

Your question is about that fact, that "permutations of the rest" is that recursive part. Recursion always consist of two parts: trivial case and recursion case. Trivial case points to a case when there's no continue for recursion and something should be returned.

在你的样品,琐碎的部分是 {A} - 只有一种排列这一套 - 本身。递归部分将是目前的元素和工会这个剩余部分 - 也就是说,如果你有一个以上的元素,那么你的结果将是这个元素和剩余部分之间的置换结合。在置换条款剩下的部分是当前的一组没有选择的元素。即对于集 {A,B,C} 第一次递归的步骤,将 {A} 和剩余部分 : {B,C} ,然后 {B} 和剩余部分: { A,C} - ,最后, {c}里与剩余部分: {A,B}

In your sample, trivial part would be {A} - there's only one permutation of this set - itself. Recursion part will be union of current element and this "rest part" - i.e. if you have more than one element, then your result will be union of permutation between this element and "rest part". In terms of permutation: the rest part is current set without selected element. I.e. for set {A,B,C} on first recursion step that will be {A} and "rest part": {B,C}, then {B} and "rest part": {A,C} - and, finally, {C} with "rest part": {A,B}

所以,你的递归将持续到时刻其余部分将单一元素 - 然后就会结束

So your recursion will last till the moment when "the rest part" will be single element - and then it will end.

这篇关于如何在算法数组的递归打印排列的工作是什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆