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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
S
Schneier on Security
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
The Register - Security
The Register - Security
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园_首页
I
Intezer
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Project Zero
Project Zero
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
Vercel News
Vercel News
A
Arctic Wolf
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
P
Privacy International News Feed
G
GRAHAM CLULEY
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Latest
Security Latest
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog

博客园 - 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()用的什么排序法,欢迎大家讨论。