C#冒泡排序和直接插入排序

/// <summary>
        /// 冒泡排序
        /// </summary>
        public static void Mainsdfdrt()
        {
            bool B;
            int[] Array = new int[] { 5, 4, 3, 2, 1 };
            for (int i = 1; i < Array.Length; i++)
            {
                B = false;
                for (int j = Array.Length-1; j >= i ; j--)
                {
                    int R;
                    if (Array[j-1]>Array[j])
                    {
                        R = Array[j - 1];
                        Array[j - 1] = Array[j];
                        Array[j] = R;
                        B = true;
                    }
                }
                if (!B)
                {
                    break;
                }
            }

            for (int i = 0; i < Array.Length; i++)
            {
                Console.WriteLine(Array[i]);
            }

            Console.ReadLine();
        }

/// <summary>
        /// 直接插入排序法
        /// </summary>
        public static void Main()
        {
            int[] R = new int[] { 5, 4, 3, 2, 1};
            //int i, j;
            for (int i = 1; i < R.Length; i++)
            {
                if (R[i] < R[i - 1])
                {
                    int T = R[i];                    
                    int j;

                    for (j = i - 1; j >= 0 && T < R[j]; j--)
                    {
                        R[j + 1] = R[j];
                    }
                    R[j + 1] = T;
                }
            }

            for (int i = 0; i < R.Length; i++)
            {
                Console.WriteLine(R[i]);
            }
            Console.ReadLine();   
        }

原文地址:https://www.cnblogs.com/huyueping/p/3278062.html