几种基本内部排序算法的比较
首先是一个装载测试数据和实现排序算法的类:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6
7 namespace SortDemo
8 {
9 public class CArrary:ICloneable
10 {
11 private int[] arr;
12 private int upper;
13 private int numElements;
14 ///<summary>
15 /// 构造函数,初始化整形数组。
16 ///</summary>
17 ///<param name="size"></param>
18 public CArrary(int size)
19 {
20 arr=new int[size];
21 upper = size - 1;
22 numElements = 0;
23 }
24 public int Length
25 {
26 get { return arr.Length; }
27 }
28 public void Insert(int item)
29 {
30 arr[numElements]=item;
31 numElements++;
32 }
33 ///<summary>
34 /// 用于显示数组。
35 ///</summary>
36 public void DisplayElements()
37 {
38 for (int i = 0; i <= upper; i++)
39 {
40 Console.Write(arr[i]+"");
41 }
42 Console.WriteLine();
43 }
44
45 #region 排序算法
46 ///<summary>
47 /// 冒泡排序
48 ///</summary>
49 public void BubbleSort()
50 {
51 int temp;
52 for (int outer = upper; outer >= 1; outer--)
53 {
54 for (int inner = 0; inner < upper;inner++ )
55 {
56 if (arr[inner] > arr[inner + 1])
57 {
58 temp=arr[inner];
59 arr[inner]=arr[inner+1];
60 arr[inner + 1] = temp;
61 }
62 }
63 }
64 }
65 ///<summary>
66 /// 快速排序
67 ///</summary>
68 ///<param name="low"></param>
69 ///<param name="high"></param>
70 public void QuicSort(int low,int high)
71 {
72
73 if (low < high)
74 {
75 int i = low, j = high, temp = arr[i];
76 while(i<j)
77 {
78 while (i < j && arr[j] >=temp)
79 {
80 j--;
81 }
82 arr[i] = arr[j];
83 while(i<j&&arr[i]<=temp)
84 {
85 i++;
86 }
87 arr[j] = arr[i];
88 }
89 arr[i] = temp;
90 QuicSort(low, i - 1);
91 QuicSort(i + 1, high);
92 }
93
94 }
95 ///<summary>
96 /// 插入排序
97 ///</summary>
98 public void InsertSort()
99 {
100 int temp, inner;
101 for (int outer = 1; outer <= upper; outer++)
102 {
103 temp = arr[outer];
104 inner = outer - 1;
105 while (inner >=0 && temp <arr[inner])
106 {
107 arr[inner + 1] = arr[inner];
108 inner--;
109 }
110 arr[inner + 1] = temp;
111 }
112 }
113 ///<summary>
114 /// 希尔排序
115 ///</summary>
116 public void ShellSort()
117 {
118
119 int j, temp;
120 for (int d =arr.Length / 2; d >= 1; d=d/2)
121 {
122 for (int i = d; i < arr.Length;i++ )
123 {
124 temp=arr[i];
125 j = i - d;
126 while(j>=0&&temp<arr[j])
127 {
128 arr[j+d]=arr[j];
129 j = j - d;
130 }
131 arr[j + d] = temp;
132 }
133 }
134 }
135 ///<summary>
136 /// 选择排序
137 ///</summary>
138 public void SelectionSort()
139 {
140 int k,temp;
141 for (int i = 0; i<=upper;i++ )
142 {
143 k = i;
144 for (int j = i + 1; j <=upper; j++)
145 {
146 if (arr[j] <=arr[k])
147 {
148 k = j;
149 }
150
151 }
152 if (i != k)
153 {
154 temp = arr[i];
155 arr[i] = arr[k];
156 arr[k] = temp;
157 }
158 }
159 }
160 #endregion
161
162 ///<summary>
163 /// 克隆。
164 ///</summary>
165 ///<returns></returns>
166 public object Clone()
167 {
168 CArrary carr=new CArrary(Length) ;
169 for (int i = 0; i < Length; i++)
170 {
171 carr.Insert(arr[i]);
172 }
173 return carr;
174 }
175
176 }
177 }
然后是一个简单的测试时间的类:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Diagnostics;
6
7 namespace SortDemo
8 {
9 ///<summary>
10 /// .NET下用于简单时间测试的类
11 ///</summary>
12 public class Timing
13 {
14 TimeSpan duration;
15 public Timing()
16 {
17 duration = new TimeSpan(0);
18 }
19 public void StopTime()
20 {
21 duration = Process.GetCurrentProcess().TotalProcessorTime;
22 }
23 public void StartTime()
24 {
25 GC.Collect();
26 GC.WaitForPendingFinalizers();
27 }
28 public TimeSpan Result()
29 {
30 return duration;
31 }
32 }
33 }
最后是进行测试的主函数:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace SortDemo
{
class Program
{
public void d()
{
}
static void Main(string[] args)
{
Timing timer;
CArrary arr = new CArrary(10000);
Random r= new Random();
for (int i = 0; i < 10000; i++)
{
arr.Insert(r.Next(10000));
}
int high = arr.Length - 1;
try
{
/*QuickSort*/
CArrary arr2 = (CArrary)arr.Clone();
timer = new Timing();
timer.StartTime();
arr2.QuicSort(0, high);
timer.StopTime();
//arr.DisplayElements();
Console.WriteLine("the time of QuickSort is:{0}", timer.Result().TotalSeconds.ToString());
/*ShellSort*/
CArrary arr3 = (CArrary)arr.Clone();
timer = new Timing();
timer.StartTime();
arr3.ShellSort();
timer.StopTime();
//arr3.DisplayElements();
Console.WriteLine("the time of ShellSort is:{0}", timer.Result().TotalSeconds.ToString());
/*InsertSort*/
CArrary arr5 = (CArrary)arr.Clone();
timer = new Timing();
timer.StartTime();
arr5.InsertSort();
timer.StopTime();
//arr5.DisplayElements();
Console.WriteLine("the time of InsertSort is:{0}", timer.Result().TotalSeconds.ToString());
/*SelectionSort*/
CArrary arr4 = (CArrary)arr.Clone();
timer = new Timing();
timer.StartTime();
arr4.SelectionSort();
timer.StopTime();
//arr.DisplayElements();
Console.WriteLine("the time of SelectionSort is:{0}", timer.Result().TotalSeconds.ToString());
/*BubbleSort*/
CArrary arr1 = (CArrary)arr.Clone();
timer = new Timing();
timer.StartTime();
arr1.BubbleSort();
timer.StopTime();
//arr.DisplayElements();
Console.WriteLine("the time of BubbleSort is:{0}", timer.Result().TotalSeconds.ToString());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
}
}
500条数据的的测试结果:

10000条数据的测试结果:

100000条数据的结果:

因为冒泡排序实在太慢,加上我这小本本性能一般,所以就不等了,但是结果可想而知。


浙公网安备 33010602011771号