阿里云折扣码

轻云博客 > C#技术 > 三大初级排序算法

三大初级排序算法

作者:Aisencici / 日期:2014-5-21 17:21:00 / 分类:C#技术 / 浏览:3285

三大初级排序算法


1、冒泡排序
      冒泡排序是最慢的排序算法。在实际运用中它是效率最低的算法。它通过一趟又一趟地比较数组中的每一个元素,使较大的数据下沉,较小的数据上升。它是O(n^2)的算法。
2、插入排序
      插入排序通过把序列中的值插入一个已经排序好的序列中,直到该序列的结束。
3、Shell排序
      Shell排序通过将数据分成不同的组,先对每一组进行排序,然后再对所有的元素进行一次插入排序,以减少数据交换和移动的次数。平均效率是O(nlogn)。

 

冒泡排序C#实现:

复制代码

    ///     /// 冒泡排序    ///     public class BubbleSort : ISort
    {        public int[] Sort(int[] array)
        {            if (array != null)
            {                for (int i = 0; i < array.Length; i++)
                {                    for (int j = 1; j < array.Length - i; j++)
                    {
                        Swap(ref array[j - 1], ref array[j]);
                    }
                }
            }            return array;
        }        public static void Swap(ref int int1, ref int int2)
        {            if (int1 > int2)
            {                int temp = int1;
                int1 = int2;
                int2 = temp;
            }
        }
    }

复制代码

插入排序C#实现:

复制代码

    ///     /// 插入排序    ///     public class InsertSort : ISort
    {        public int[] Sort(int[] array)
        {            if (array != null)
            {                int k = 1;//使用k变量,后面更好的扩展到Shell排序
                for (int i = k; i < array.Length; i++)
                {                    int current = array[i];                    int preIndex = i - k;                    while (preIndex >= 0 && preIndex < array.Length && current < array[preIndex])
                    {
                        array[preIndex + k] = array[preIndex];

                        preIndex = preIndex - k;
                    }
                    array[preIndex + k] = current;
                }
            }            return array;
        }
    }

复制代码

Shell排序C#实现:

复制代码

    ///     /// shell排序    ///     public class ShellSort : ISort
    {        public int[] Sort(int[] array)
        {            if (array != null)
            {                int[] list = { 9, 5, 3, 2, 1 };                foreach (int k in list)
                {                    for (int i = k; i < array.Length; i++)
                    {                        int current = array[i];                        int preIndex = i - k;                        while (preIndex >= 0 && preIndex < array.Length && current < array[preIndex])
                        {
                            array[preIndex + k] = array[preIndex];

                            preIndex = preIndex - k;
                        }
                        array[preIndex + k] = current;
                    }
                }
            }            return array;
        }
    }

复制代码

性能测试代码:

复制代码

    class Program
    {        public static Random re = new Random();        static void Main(string[] args)
        {
            Stopwatch stw1 = new Stopwatch();
            Stopwatch stw2 = new Stopwatch();
            Stopwatch stw3 = new Stopwatch();            int[] intArray1 = GetArray(int.MaxValue/100000);            int[] intArray2 = GetArray(int.MaxValue/100000);            int[] intArray3 = GetArray(int.MaxValue/100000);

            ISort sort1 = new BubbleSort();//冒泡排序            stw1.Start();            int[] result1 = sort1.Sort(intArray1);
            stw1.Stop();
            Console.WriteLine("输出排序的结果(冒泡排序)");
            Console.WriteLine("程序共运行时间:" + stw1.Elapsed.ToString());

            ISort sort2 = new InsertSort();//插入排序            stw2.Start();            int[] result2 = sort2.Sort(intArray2);
            stw2.Stop();
            Console.WriteLine("输出排序的结果(插入排序)");
            Console.WriteLine("程序共运行时间:" + stw2.Elapsed.ToString());

            ISort sort3 = new ShellSort();//Shell排序            stw3.Start();            int[] result3 = sort3.Sort(intArray3);
            stw3.Stop();
            Console.WriteLine("输出排序的结果(Shell排序)");
            Console.WriteLine("程序共运行时间:" + stw3.Elapsed.ToString());            //输出排序的结果            //OutputResult(result1, result2, result3);
            Console.ReadKey();
        }
    }

复制代码

结果截图:

 

 


本文标签:排序算法
From:http://www.cnblogs.com/lmfeng/p/3741098.html
分享到: