数据结构(C#)_排序算法(归并排序)
还有半天就是周末了,一个星期又过去了,嘿嘿。周末可以好好休息一下了。今天我们讲的是归并排序,首先简单介绍一下概念。
归并排序:归并的含义就是将两个或者两个以上的有序表组合成一个新的有序表。归并排序是一种稳定的排序方法。下面我们就来看一下代码:
1
namespace Sorting
2
{
3
class SortingAlgorithms
4
{
5
private int[] arr;
6
private int upper;
7
private int numElement;
8![](/Images/OutliningIndicators/InBlock.gif)
9
//初始化数组
10
public SortingAlgorithms(int size)
11
{
12
arr = new int[size];
13
upper = size - 1;
14
numElement = 0;
15
}
16![](/Images/OutliningIndicators/InBlock.gif)
17
//给数组插入元素
18
public void Insert(int item)
19
{
20
arr[numElement] = item;
21
numElement++;
22
}
23![](/Images/OutliningIndicators/InBlock.gif)
24
//打印数组元素
25
public void DisplayElement()
26
{
27
for (int i = 0; i <= upper; i++)
28
{
29
Console.Write(arr[i] + " ");
30
}
31
Console.ReadLine();
32
}
33![](/Images/OutliningIndicators/InBlock.gif)
34
public void CleaArry()
35
{
36
for (int i = 0; i <= upper; i++)
37
{
38
arr[i] = 0;
39
numElement = 0;
40
}
41
}
42![](/Images/OutliningIndicators/InBlock.gif)
43
/*归并排序,以最坏的运行时间运行,该算法的基本的操作是合并两个
44
已经排序的表,是一个稳定的排序算法,但是不经常使用。
45
*/
46
public void MergeSort()
47
{
48
//临时数组,存放排序好的数据
49
int [] TempArray = new int[numElement];
50
RecMergeSort(TempArray, 0, numElement - 1);
51![](/Images/OutliningIndicators/InBlock.gif)
52
}
53![](/Images/OutliningIndicators/InBlock.gif)
54
public void RecMergeSort(int [] tempArray,int lbount,int ubound)
55
{
56
if (lbount == ubound)
57
{
58
return;
59
}
60
else
61
{
62
int Mid = (lbount + ubound) / 2;
63
//递归调用
64
//数组的前半段
65
RecMergeSort(tempArray,lbount,Mid);
66
//数组的后半段
67
RecMergeSort(tempArray, Mid + 1, ubound);
68
Merge(tempArray, lbount, Mid + 1, ubound);
69
}
70
71
}
72![](/Images/OutliningIndicators/InBlock.gif)
73
public void Merge(int [] tempArray,int lowp,int highp,int ubound)
74
{
75
int lbound = lowp;
76
int mid = highp - 1;
77
int n = (ubound - lbound) + 1;
78
//原书中没有定义该变量,但是在while语句中用到了
79
int j = 0;
80
//将数组中元素由小到大复制到临时数组
81
while ((lowp <= mid) && (highp <= ubound))
82
{
83
if (arr[lowp] < arr[highp])
84
{
85
tempArray[j] = arr[lowp];
86
j++;
87
lowp++;
88
}
89
else
90
{
91
tempArray[j] = arr[highp];
92
j++;
93
highp++;
94
}
95
}
96![](/Images/OutliningIndicators/InBlock.gif)
97
//拷贝上半部的数据到临时数组
98
while (lowp <= mid)
99
{
100
tempArray[j] = arr[lowp];
101
j++;
102
lowp++;
103
}
104![](/Images/OutliningIndicators/InBlock.gif)
105
//拷贝下半部的剩余数据到临时数组
106
while (highp <= ubound)
107
{
108
tempArray[j] = arr[highp];
109
j++;
110
highp++;
111
}
112![](/Images/OutliningIndicators/InBlock.gif)
113
//原书中还是定义的为j,这样容易造成歧义,修改为k
114
//将临时数组中有序的表拷贝至正式数组中
115
for (int k = 0; k <= n - 1; k++)
116
{
117
arr[lbound + k] = tempArray[k];
118
}
119
}
120![](/Images/OutliningIndicators/InBlock.gif)
121
}
122![](/Images/OutliningIndicators/InBlock.gif)
123
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
119
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以下是调用的代码:
1
static void Main(string[] args)
2
{
3
SortingAlgorithms MyArray = new SortingAlgorithms(10);
4
Random rnd = new Random(100);
5![](/Images/OutliningIndicators/InBlock.gif)
6
long Ticks = DateTime.Now.Ticks;
7
for (int i = 0; i < 10; i++)
8
{
9
MyArray.Insert((int)(rnd.NextDouble() * 100));
10
}
11![](/Images/OutliningIndicators/InBlock.gif)
12
Console.WriteLine("Before Sorting:");
13
MyArray.DisplayElement();
14![](/Images/OutliningIndicators/InBlock.gif)
15
//归并排序
16
MyArray.MergeSort();
17![](/Images/OutliningIndicators/InBlock.gif)
18
Console.WriteLine("After sorting");
19
//打印排序后的元素
20
MyArray.DisplayElement();
21![](/Images/OutliningIndicators/InBlock.gif)
22
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)