各种排序方法的性能比较

测试环境说明:

Win-XP下,VS2008,主频:core2 双核2.53GHZ

 

下面是测试的代码:

 

 

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

namespace TestSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int MaxValue = 10000;
            int[] sortPre = GenerateintList(MaxValue);
            Console.WriteLine("随机数产生完毕!");


            //DateTime SelectPreTime = System.DateTime.Now;
            //SelectSort(sortPre);
            //DateTime SelectAfterTime = System.DateTime.Now;

            //TimeSpan Selectspan = SelectAfterTime - SelectPreTime;

            DateTime ShellPreTime = System.DateTime.Now;
            ShellSort(sortPre);
            DateTime ShellAfterTime = System.DateTime.Now;
           
            TimeSpan Shellspan = ShellAfterTime - ShellPreTime;
           

            //DateTime RaisePreTime = System.DateTime.Now;
            //RaiseSort(sortPre);
            //DateTime RaiseAfterTime = System.DateTime.Now;

            //TimeSpan Raisespan = RaiseAfterTime - RaisePreTime;


            //Console.WriteLine("选择排序用时:" + Selectspan.TotalMilliseconds.ToString() + "毫秒");
            Console.WriteLine("希尔排序用时:" + Shellspan.TotalMilliseconds.ToString() + "毫秒");

            //Console.WriteLine("冒泡排序用时:" + Raisespan.TotalMilliseconds.ToString() + "毫秒");
            Console.ReadKey();
        }

        //产生随机数
        private static int[] GenerateintList(int maxValue)
        {
            int[] intArr = new int[maxValue];
            ArrayList intList = new ArrayList(maxValue);
            Random random = new Random();
            for (; intList.Count < intList.Capacity; )
            {
                int cellint = random.Next(1, maxValue + 1);
                if (!intList.Contains(cellint))
                    intList.Add(cellint);
            }
            for (int index = 0; index < intList.Capacity; index++)
            {
                intArr[index] = (int)intList[index];
            }
            return intArr;
        }

        //选择排序

        public static void SelectSort(int[] sourceArr)
        {
            int i = 0, j = 0;
            for (; i < sourceArr.Length; i++)
            {
                j = i + 1;
                for (; j < sourceArr.Length; j++)
                {
                    if (sourceArr[j] < sourceArr[i])
                    {
                        int temp = sourceArr[j];
                        sourceArr[j] = sourceArr[i];
                        sourceArr[i] = temp;
                    }
                }
            }
        }

        //希尔排序
        public static void ShellSort(int[] list)
        {
            int inc;
            for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= list.Length; i += inc)
                {
                    int t = list[i - 1];
                    int j = i;
                    while ((j > inc) && (list[j - inc - 1] > t))
                    {
                        list[j - 1] = list[j - inc - 1];
                        j -= inc;
                    }
                    list[j - 1] = t;
                }
            }
        }

        //冒泡排序

        public static void RaiseSort(int[] list)
        {
            for(int j = 1 ; j<list.Length;j++)
            {
                for(int i=0;i<list.Length-1;i++)
                {
                    if(list[i]>list[i+1])
                    {
                        int temp = list[i+1];
                        list[i+1] = list[i];
                        list[i] = temp;
                    }
                }
            }
        }
    }
}

 

 

 

 

希尔排序测试了3次,分别是:156.26毫秒、140.625毫秒、140.625毫秒;  平均:145.84毫秒

选择排序测试了3次,分别是:437.5毫秒、437.5毫秒、453.125毫秒,平均:442.71毫秒

冒泡排序测试了3次,分别是:609.375毫秒、609.375毫秒、609.375毫秒,平均:609.375毫秒

 

 

 

结论:希尔排序法 >> 选择排序法 > 冒泡排序法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值