Home > Backend Development > C++ > How Can Recursive Algorithms Generate All Permutations of Strings and Integers?

How Can Recursive Algorithms Generate All Permutations of Strings and Integers?

DDD
Release: 2025-01-30 08:36:13
Original
972 people have browsed it

How Can Recursive Algorithms Generate All Permutations of Strings and Integers?

The arrangement algorithm of the string and integer

In programming interviews, a common challenge is to generate all possible arranges of a given string or integer. This may involve recursive use.

Understand Principles

Recursively include two key steps:

Initial steps: For a single element, arrangement is the element itself.
  1. Sub -step: For the element collection, each arrangement combination includes each element, and the surplus element.
  2. Human language example

A single element:

Two elements:

<code>perm(a) -> a</code>
Copy after login

Three elements:

<code>perm(ab) ->
a + perm(b) -> ab
b + perm(a) -> ba</code>
Copy after login

The recursive algorithm in the pseudo code

C# implementation
<code>perm(abc) ->
a + perm(bc) -> abc, acb
b + perm(ac) -> bac, bca
c + perm(ab) -> cab, cba</code>
Copy after login

This C# realizes recursive and exchange to effectively generate all arrangements.
<code>generatePermutations(permutation) {
  if (permutation 的长度 为 0) {
    打印 permutation
    返回
  }
  对于 permutation 中的每个元素 element:
    创建一个新的排列 newPermutation,移除 element
    将 element 添加到 generatePermutations(newPermutation) 的结果的前面
}</code>
Copy after login
The two elements in the array of the function switch, while the recursive function traverses all possible arrangements. Backback steps () ensure that the array returns to the previous state after processing a arrangement in order to generate the next arrangement.

This Revied Answer Provides a More Concise and Accurate Explanation of the Recursive Permutation Algorithm, Including A Clearer Pseudocode Reposition and AN Functional C# Implementation with Comments. The Image Remains in ITS Original format and local.
public static void GeneratePermutations(char[] list) {
    int x = list.Length - 1;
    GeneratePermutations(list, 0, x);
}

private static void GeneratePermutations(char[] list, int k, int m) {
    if (k == m) {
        Console.WriteLine(new string(list)); //输出排列
    } else {
        for (int i = k; i <= m; i++) {
            Swap(list, i, k);
            GeneratePermutations(list, k + 1, m);
            Swap(list, i, k); // 回溯,恢复原始顺序
        }
    }
}

private static void Swap(char[] list, int i, int j) {
    char temp = list[i];
    list[i] = list[j];
    list[j] = temp;
}
Copy after login

The above is the detailed content of How Can Recursive Algorithms Generate All Permutations of Strings and Integers?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template