三位数全排序

时间:2023-01-31 00:13:47

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace quanpaixu

{

    class Program {

         static void Main(string[] args)

            {

               int[] a = new int[] { 5, 4, 3 }; //全排序数组

               Program s = new Program();

               int i=0;

               s.paixu(a,i); //全排序

            }

          public void paixu(int[] a,int i) //把全排序数组分割成一个单独的数和一个长度减小1的数组(i为坐标)

            {

               int t = 0; int c = 0;

               int[] temp = new int[a.Length-1]; //存储分割后的数组

               int b = 0;

               for (int k = 0; k < a.Length; k++)

                 {

                   if (k == i)

                     {

                        t = a[k]; //取坐标为i的值,根据此值开始运算

                      }

                   else

                      { temp[b] = a[k];

                        b = b + 1;

                      }

                   }

                    insert(temp,t,c); //排序数组

                    i = i + 1; //遍历,长度超过数组长度时,跳出

                    if (i > a.Length - 1) { return; }

                    paixu(a, i);

                }

              public void insert(int[] a1,int t,int c)

                {

                   int[] b = new int[a1.Length + 1]; b[0] = t;

                       if (c == 0) //不同的排序情况

                           { for (int j = 0; j < a1.Length; j++) { b[j + 1] = a1[j]; } }

                      else { for (int k = 0; k < a1.Length; k++) { b[k + 1] = a1[1-k]; } }

                   for (int d = 0; d < b.Length; d++)//输出遍历结果

                       { Console.Write(b[d]+" "); }

                   Console.WriteLine();

                  c = c + 1; //生出不同的排序结果,遍历

                 if (c > 1) { return; }

                  insert(a1, t, c);

} } }