C# 插入排序算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] a = { 118, 101, 105, 127, 112 };

            InsertSort(a);



            Console.ReadLine();
        }



        static void InsertSort(int[] array)
        {
            int j, t;

            for (int i = 1; i < array.Length; i++)
            {
                t = array[i];
                j = i - 1;


                while (j >= 0 && t < array[j]) 
                {
                    array[j + 1] = array[j];
                    j--;
                }

                array[j + 1] = t;

                Console.WriteLine("" + i + "步排序结果");

                for (int h = 0; h < array.Length; h++)
                {
                    Console.Write(" " + array[h].ToString());
                }
                Console.WriteLine();
            }


        }

    
    }

    


}

image

posted @ 2016-05-30 22:56  盘子脸  阅读(722)  评论(0编辑  收藏  举报