SoFunction
Updated on 2025-03-07

C# recursive algorithm and permutation algorithm

1. Recursive algorithm

recursion:You open the door in front of you and see that there is another door inside the house. You walk over and find that the key in your hand can also open it. You push open the door and find that there is another door inside, and you continue to open it. Several times later, after you opened the door in front of you, you found that there was only one room and no door. Then, you start to return the same way. Every time you walk back to a room, you count it, and when you reach the entrance, you can answer how many doors you have opened with this key.

cycle:You open the door in front of you and see that there is another door inside the house. You walk over and find that the key in your hand can open it. When you push open the door, you find that there is another door inside (if the front two doors are the same, then this door is the same as the first two doors; if the second door is smaller than the first door, then this door is smaller than the second door. You continue to open the door and continue like this until all the doors are opened. However, the person at the entrance can never wait for you to go back and tell him the answer.

1. Definition:

In mathematics and computer science, recursion refers to the method of using the function itself in the definition of a function. In fact, recursion, as the name suggests, contains two meanings: relay and return, which is the essence of recursive thought.

2. Example:

static void  Main(string[] args)
{
    int[] sum = new int[30];
    for (int i = 0; i < ; i++)
    {
        sum[i] = process1(i);
        (sum[i]);
    }
}
public static int process1(int a)
{
    if (a == 0 || a == 1) return 1;

    return process1(a - 1) + process1(a - 2);
}

3. Factorial algorithm:

public static int process2(int n)
{
    if (n == 1) return 1;

    return n * process2(n - 1); // Same duplicate logic to reduce the size of the problem}

2. Alignment algorithm

Output a full array of any letters and numbers

For a string of length n or a string array composed of n characters (numbers, nodes), its entire arrangement has A(n, n)=n!. This problem is also a recursive problem. For example, 1, 2, 3, the full arrangement can be obtained: {123, 132, 213, 231, 312, 321}.

The code is implemented using a recursive algorithm as follows:

      public static void Permutation(string[] nums, int m, int n)
      {
         string t;
         if (m &lt; n - 1)
         {
            Permutation(nums, m + 1, n);
            for (int i = m + 1; i &lt; n; i++)
            {
               //Swap method can be extracted               t = nums[m];
               nums[m] = nums[i];
               nums[i] = t;
               Permutation(nums, m + 1, n);

               //Swap method can be extracted               t = nums[m];
               nums[m] = nums[i];
               nums[i] = t;
            }
         }
         else
         {
                #region Save on List                Node root = null;
                Node currentNode;
                for (int j = 0; j &lt; ; j++)
                {
                    currentNode = new Node(nums[j]);
                     = root;
                    root = currentNode;
                }
                (root);
                #endregion


                #region Print console                for (int j = 0; j &lt; ; j++)
                {
                    (nums[j]);
                }
                ();

                #endregion
         }
      }

Calling algorithm:

      static void Main(string[] args)
      {
         Nums = new string[] { "a", "b", "c" };
         Permutation(Nums, 0, );
         ();
      }

This is all about this article about recursion and arrangement of C# algorithms. I hope it will be helpful to everyone's learning and I hope everyone will support me more.