你知道C#排序的编写及性能吗?

开发 后端
本文将带大家复习了一下C#的数据结构中有关C#排序算法,其中主要有冒泡排序,直接插入排序,简单选择排序和快速排序。

在进行C#排序算法之前,我们先定义一个100位的随机数列,好进行各种排序算法的性能测试。

代码如下:

 /// 
  /// 随机生成100位的数组
/// 
  /// 返回生成数组
  public static int[] RandomArray()
  {
Random ran = new Random();
int[] arr = new int[100];
int tem;
for (int i = 0; i < 100; i++)
{
 tem = ran.Next(1, 100);
 arr[i] = tem;
}
return arr;
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

1.冒泡排序 (Bubble Sort)

基础思想:将相邻的记录的关键码进行比较,若前面记录的关键码大于后面记录的关键码,则将它们交换,否则不交换。

代码如下:

/// 
 /// 冒泡排序算法
 /// 
 public class BubbleSort : IAction
 {
  #region IAction 成员
  public void Action()
  {
int[] array = Program.RandomArray();
for (int a = 0; a < array.Length; a++)
{
 int item = 0;
 for (int b = array.Length - 1; b > a; b--)
 {
  if (array[b] < array[b - 1])
  {
item = array[b];
array[b] = array[b - 1];
array[b - 1] = item;
  }
 }
}
  }
  #endregion
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

2.直接插入排序

基础思想: 顺序的将待排序的记录安关键码的大小插入到已排序的记录子序列的适当位置。子序列的记录个数从1开始逐渐增大,当子序列记录个数于首先表中的记录个数相同时排序完毕。

代码如下: 

/// 
 /// 直接插入排序算法
 /// 
 public class DirectInsertSort : IAction
 {

  #region IAction 成员

  public void Action()   { int[] array = Program.RandomArray(); for (int i = 1; i < array.Length; i++) {  if (array[i] < array[i - 1])  {   int tem = array[i];   int j = 0;   for (j = i - 1; j >= 0 && tem < array[j]; j--)   { array[j + 1] = array[j];   }   array[j + 1] = tem;  } }   }   #endregion  }

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

3.简单选择排序

基础思想:从待排序的记录序列中选择关键码最小(或)最大的记录并将它也序列中的第一个记录交换位置;然后从不包括第一个位置上的记录序列中选择关键码最小(或最大)的记录并将它也序列中的第2个记录交换位置,如此重复,直到序列只剩下一个记录为止。

代码如下:

/// 
 /// 简单选择排序算法
 /// 
 public class SimpleSelectSort : IAction
 {
  #region IAction 成员
  public void Action()
  {
int[] array = Program.RandomArray();
 int tmp = 0;
 int t = 0;
 for (int i = 0; i < array.Length; i++)
 {
  t = i;
  for (int j = i + 1; j < array.Length; j++)
  {
if (array[t] > array[j])
{
 t = j;
}
  }
  tmp = array[i];
  array[i] = array[t];
  array[t] = tmp;
  }
  #endregion
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

4.快速排序

基础思想:通过不断比较关键码,以某咯记录为界(该记录成为支点),将待排序列分成两部分。其中,一小部分满足所有记录的关键码都大于或等于支点记录为界将待排序列按关键码中分成两部分的过程,称为一次划分,直到整个序列按关键码有序为止。

代码如下:

 

 ///

 /// 快速排序算法  ///  public class QuickSort : IAction  {

  #region IAction 成员

  public void Action()   { int[] array = Program.RandomArray();

QuickSortArray(array, 0, array.Length - 1);   }

  private void QuickSortArray(int[] arr, int low, int high)   { int i = low; int j = high; int tmp = arr[low]; while (low < high) {  while ((low < high) && arr[high] >= tmp)  {   --high;  }  arr[low] = arr[high];  while ((low < high) && arr[low] <= tmp)  {   ++low;  }  arr[high] = arr[low];  --high; } arr[low] = tmp; if (i < low - 1) {  QuickSortArray(arr, i, low - 1); } if (j > low + 1) {  QuickSortArray(arr, low + 1, j); }   }

  #endregion  }

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.

通过上面的描述,我们对C#几个主要的排序算法了解清楚了 接下来开始解析它们其中的奥妙吧。其实在此之前 我一直都喜欢使用冒泡来排序,不过通过CodeTimer测试了下  结果如下:

C#排序结果

C#排序结果

总结:从C#几个常用的排序算法中,我们常用的冒泡法消耗的系统时间是最多了,相比之下快速排序所用的时间倒是很快速。

希望能和大家一起探讨.NET中的神奇,享受编程给我们带来的乐趣~

【编辑推荐】

  1. C# 4.0中泛型协变性和逆变性详解
  2. C#中用鼠标移动页面功能的实现
  3. C# 2010命名和可选参数的新特性
  4. 讨论:C# 4.0新特性dynamic有何用?
  5. C# 4.0新特性dynamic作用浅析
责任编辑:彭凡 来源: cnblogs
相关推荐

2024-09-12 08:20:39

2024-11-01 10:48:01

C#WPF程序

2021-04-13 05:36:18

C#null 可控

2018-11-21 10:47:46

排序算法TimsortPython

2021-04-14 06:53:52

C# 修饰符 Public

2024-04-23 08:08:04

C#

2021-07-27 06:49:11

C#存储检索

2021-07-11 18:04:04

C语言

2024-05-10 07:44:23

C#进程程序

2024-06-28 09:37:14

技巧.NET开发

2024-04-24 11:24:43

C#数据去重

2024-04-17 08:05:18

C#并发设计

2023-09-26 07:38:53

c#Lambda表达式

2020-07-15 14:51:39

代码C+开发

2013-03-19 09:48:38

C#

2024-09-03 08:06:30

AQS线程代码

2020-05-15 14:34:16

C语言丹尼斯 · 里奇开发者

2009-08-28 15:05:35

C#编写Calenda

2009-08-25 15:38:12

C# Windows服

2009-08-18 17:08:50

C#编写XML文档
点赞
收藏

51CTO技术栈公众号