您的位置:首页 > 编程语言 > C#

C#插入排序

2008-09-04 23:17 260 查看
using System;
namespace InsertionSort
{
    /// <summary>
    /// 插入排序
    /// </summary>
    public class InsertionSort
    {
        public void Sort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int temp = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > temp))
                {
                    list[j] = list[j - 1];
                    j--;
                }
                list[j] = temp;
            }
        }
        static void Main(string[] args)
        {
            int[] test = new int[] { 1, 6, 3, 8, 11, 43, 0, 3, 57 };
            InsertionSort its = new InsertionSort();
            its.Sort(test);
            for (int i = 0; i < test.Length - 1; i++)
            {
                Console.WriteLine("{0}", test[i]);
            }
            Console.ReadKey();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: