c# 插入排序

 class Program32
    {
        public static void Sort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                int tempInt = 0;
                while ((j>0)&&(list[j-1]>t)) //如果前一个元素大于当前元素,则交换
                {
                    //大数依次往后排
                    list[j] = list[j-1];
                    --j;
                    tempInt++;
                    Console.WriteLine("while循环次数:" + tempInt);
                }
                list[j] = t;
                Console.WriteLine("" + i + "次排序后的结果:");
                foreach (var item in list)
                {
                    Console.Write(item + " ");
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            int[] iArrary = new int[] { 100, 98, 97, 96, 95, 94 };
            Sort(iArrary);
            Console.ReadLine();
        }
    }

 

posted @ 2018-11-05 20:31  幸福安康  阅读(119)  评论(0编辑  收藏  举报