C#插入排序原理讲解及代码块

1.原理讲解
插入排序原理讲解

2.代码块

    /// 
    /// 直接插入排序算法
    /// 
    public class InsertSortAlgorithm
    {
       static int index = 0;
        /// 
        /// 插入排序
        /// 
        /// 
        /// 
        public static int[] InsertSort(int[] array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                int insertVal = array[i];
                int insertIndex = i - 1;
                while (insertIndex >= 0 && insertVal < array[insertIndex])
                {
                    array[insertIndex + 1] = array[insertIndex];
                    insertIndex--;
                    index++;
                }
                array[insertIndex + 1] = insertVal;
                printArray(array);
                Console.WriteLine("第" + (i) + "趟");
            }
            Console.Write("index:" + (index) + "次数");
            return array;
        }
        /// 
        /// 打印数组
        /// 
        /// 
        public static void printArray(int[] array)
        {
            if (array == null || array.Length <= 0)
            {
                return;
            }
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("[" + array[i] + "]" + ",");
            }
        }
    }

你可能感兴趣的:(算法排序)