惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
A
About on SuperTechFans
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
月光博客
月光博客
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
Jina AI
Jina AI
有赞技术团队
有赞技术团队
博客园_首页

博客园 - Forrest Gump

关于LINQ中数据库连接字符串的问题 项目开发经验-ASP.NET项目开发中的异常处理 关于模态窗口(showModalDialog)的专题【收藏】 C#面试题 C# 将数据导出到Excel汇总 关于Assembly.CreateInstance()与Activator.CreateInstance()方法 PowerDesigner概念设计模型(CDM)中的3种实体关系 C#基础概念二十五问 Microsoft .NET Pet Shop 4:将 ASP.NET 1.1 应用程序迁移到 2.0 用Inno Setup制作WEB程序安装包 堆排序 (Heap sort) 合并排序法(Merge Sort) 希尔排序法 quick sort 关于switch的小技巧 C#中一些很基础但有经常导致错误的一些概念 Enterprise Library Step By Step系列(十六):使用AppSetting Application Block Enterprise Library Step By Step系列(十五):配置应用程序块——设计篇 创建基于消息队列(MSMQ)的异步日志
冒泡法数组排序与 System.Array.Sort()排序性能比较
Forrest Gump · 2008-01-28 · via 博客园 - Forrest Gump


今天看c#plus 中的一个比较冒泡排序与 System.Array.Sort() 性能,发现System.Arry.Sort()的性能要远远的优于冒泡排序,下面代码大家有兴趣可以看一下:

 1using System;
 2
 3class SortingCompetition
 4{
 5    public static void BubbleSortAscending(int [] bubbles)
 6    {
 7        bool swapped = true;
 8
 9        for (int i=0;swapped ;i++ )
10        {
11            swapped = false;
12            for (int j = 0;j<(bubbles.Length-(i+1)) ;j++ )
13            {
14                if (bubbles[j]>bubbles[j+1])
15                {
16                    Swap(j,j+1,bubbles);
17                    swapped = true;
18                }

19            }

20        }

21    }

22
23    public static void Swap(int first,int second,int [] arr)
24    {
25        int temp;
26
27        temp = arr[first];
28        arr[first] = arr[second];
29        arr[second] = temp;
30    }

31
32    public static void PrintArray(int [] arr)
33    {
34        for (int i=0;i<arr.Length ;i++ )
35        {
36            Console.Write("{0}  ",arr[i]);
37        }

38    }

39
40    public static void Main()
41    {
42        int [] testScores = new int [200000];
43        DateTime sortStart;
44        DateTime sortEnd;
45
46        for (int i=0;i<testScores.Length ;i++ )
47        {
48            testScores[i] = testScores.Length-i;
49        }

50
51        Console.WriteLine("Now timing the bubble sort method please wait");
52        sortStart = DateTime.Now;
53        BubbleSortAscending(testScores);
54        sortEnd = DateTime.Now;
55        Console.WriteLine("Second elapsed bubble sorting an array of length {0}:{1}\n",testScores.Length,((sortEnd - sortStart).Ticks/10000000));
56        
57        //PrintArray(testScores);
58
59        for (int i=0;i<testScores.Length ;i++ )
60        {
61            testScores[i] = testScores.Length-i;
62        }

63
64        Console.WriteLine("\nNow timing the built in sort method of System.Array. Please wait");
65        sortStart = DateTime.Now;
66        Array.Sort(testScores);
67        sortEnd = DateTime.Now;
68        Console.WriteLine("Seconds elapsed .NET sorting an arry of length {0}:{1}\n",testScores.Length,(((sortEnd - sortStart).Ticks/10000000)));
69        
70        //PrintArray(testScores);
71    }

72}

上例中对一个长度为200000的数组进行排序,冒泡法用去107秒,System.Array.Sort()确用了不到1秒的时间,性能的差距太大了,有谁知道System.Array.Sort()用的什么排序法,欢迎大家讨论。