代码改变世界

[原创]直接插入排序InsertSort

2007-08-12 13:54  Virus-BeautyCode  阅读(476)  评论(0编辑  收藏  举报

 class InsertSort
    {
        private int[] mylist;

        public InsertSort()
        {
            mylist = new int[] {  19, 13, 5, 27, 1, 26, 31, 16, 2, 9, 11, 21 } ;
        }

        public void Start()
        {
            int T;
            for (int j =1; j < mylist.Length; j++)
            {
                T = mylist[j];
                int i = j - 1;
                while (i>=0 && mylist[i]>T)
                {
                    mylist[i+1]=mylist[i];
                    i=i-1;
                }
                mylist[i + 1] = T;
            }
        }

        public void Display()
        {
            for (int i = 0; i < mylist.Length - 1; i++)
            {
                Console.WriteLine(mylist[i].ToString());
            }
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            InsertSort insert = new InsertSort();
            insert.Start();
            insert.Display();
            Console.ReadLine();
        }
    }