还有半天就是周末了,一个星期又过去了,嘿嘿。周末可以好好休息一下了。今天我们讲的是归并排序,首先简单介绍一下概念。
归并排序:归并的含义就是将两个或者两个以上的有序表组合成一个新的有序表。归并排序是一种稳定的排序方法。下面我们就来看一下代码:
1
namespace
Sorting
2 {
3 class SortingAlgorithms
4 {
5 private int[] arr;
6 private int upper;
7 private int numElement;
8
9 //初始化数组
10 public SortingAlgorithms(int size)
11 {
12 arr = new int[size];
13 upper = size - 1;
14 numElement = 0;
15 }
16
17 //给数组插入元素
18 public void Insert(int item)
19 {
20 arr[numElement] = item;
21 numElement++;
22 }
23
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
34 public void CleaArry()
35 {
36 for (int i = 0; i <= upper; i++)
37 {
38 arr[i] = 0;
39 numElement = 0;
40 }
41 }
42
43 /**//*归并排序,以最坏的运行时间运行,该算法的基本的操作是合并两个
44 已经排序的表,是一个稳定的排序算法,但是不经常使用。
45 */
46 public void MergeSort()
47 {
48 //临时数组,存放排序好的数据
49 int [] TempArray = new int[numElement];
50 RecMergeSort(TempArray, 0, numElement - 1);
51
52 }
53
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
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
97 //拷贝上半部的数据到临时数组
98 while (lowp <= mid)
99 {
100 tempArray[j] = arr[lowp];
101 j++;
102 lowp++;
103 }
104
105 //拷贝下半部的剩余数据到临时数组
106 while (highp <= ubound)
107 {
108 tempArray[j] = arr[highp];
109 j++;
110 highp++;
111 }
112
113 //原书中还是定义的为j,这样容易造成歧义,修改为k
114 //将临时数组中有序的表拷贝至正式数组中
115 for (int k = 0; k <= n - 1; k++)
116 {
117 arr[lbound + k] = tempArray[k];
118 }
119 }
120
121 }
122
123}
2 {
3 class SortingAlgorithms
4 {
5 private int[] arr;
6 private int upper;
7 private int numElement;
8
9 //初始化数组
10 public SortingAlgorithms(int size)
11 {
12 arr = new int[size];
13 upper = size - 1;
14 numElement = 0;
15 }
16
17 //给数组插入元素
18 public void Insert(int item)
19 {
20 arr[numElement] = item;
21 numElement++;
22 }
23
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
34 public void CleaArry()
35 {
36 for (int i = 0; i <= upper; i++)
37 {
38 arr[i] = 0;
39 numElement = 0;
40 }
41 }
42
43 /**//*归并排序,以最坏的运行时间运行,该算法的基本的操作是合并两个
44 已经排序的表,是一个稳定的排序算法,但是不经常使用。
45 */
46 public void MergeSort()
47 {
48 //临时数组,存放排序好的数据
49 int [] TempArray = new int[numElement];
50 RecMergeSort(TempArray, 0, numElement - 1);
51
52 }
53
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
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
97 //拷贝上半部的数据到临时数组
98 while (lowp <= mid)
99 {
100 tempArray[j] = arr[lowp];
101 j++;
102 lowp++;
103 }
104
105 //拷贝下半部的剩余数据到临时数组
106 while (highp <= ubound)
107 {
108 tempArray[j] = arr[highp];
109 j++;
110 highp++;
111 }
112
113 //原书中还是定义的为j,这样容易造成歧义,修改为k
114 //将临时数组中有序的表拷贝至正式数组中
115 for (int k = 0; k <= n - 1; k++)
116 {
117 arr[lbound + k] = tempArray[k];
118 }
119 }
120
121 }
122
123}
以下是调用的代码:
1
static
void
Main(
string
[] args)
2 {
3 SortingAlgorithms MyArray = new SortingAlgorithms(10);
4 Random rnd = new Random(100);
5
6 long Ticks = DateTime.Now.Ticks;
7 for (int i = 0; i < 10; i++)
8 {
9 MyArray.Insert((int)(rnd.NextDouble() * 100));
10 }
11
12 Console.WriteLine("Before Sorting:");
13 MyArray.DisplayElement();
14
15 //归并排序
16 MyArray.MergeSort();
17
18 Console.WriteLine("After sorting");
19 //打印排序后的元素
20 MyArray.DisplayElement();
21
22 }
2 {
3 SortingAlgorithms MyArray = new SortingAlgorithms(10);
4 Random rnd = new Random(100);
5
6 long Ticks = DateTime.Now.Ticks;
7 for (int i = 0; i < 10; i++)
8 {
9 MyArray.Insert((int)(rnd.NextDouble() * 100));
10 }
11
12 Console.WriteLine("Before Sorting:");
13 MyArray.DisplayElement();
14
15 //归并排序
16 MyArray.MergeSort();
17
18 Console.WriteLine("After sorting");
19 //打印排序后的元素
20 MyArray.DisplayElement();
21
22 }