数据结构(C#)_排序算法(归并排序)
2020-02-12 08:05
405 查看
还有半天就是周末了,一个星期又过去了,嘿嘿。周末可以好好休息一下了。今天我们讲的是归并排序,首先简单介绍一下概念。
归并排序:归并的含义就是将两个或者两个以上的有序表组合成一个新的有序表。归并排序是一种稳定的排序方法。下面我们就来看一下代码:
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 }
转载于:https://www.cnblogs.com/chencaixia/archive/2008/03/21/1116257.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- [C++]数据结构:排序算法Part2----快速排序、归并排序、箱子排序、基数排序
- 数据结构之排序算法--C#实现
- C# 数据结构中 排序算法
- [C++]数据结构:排序算法Part2----快速排序、归并排序、箱子排序、基数排序
- 排序算法 终结版 (C#) --数据结构
- [翻译]C#数据结构与算法 – 第三章基本排序算法
- [翻译]C#数据结构与算法 – 第三章基本排序算法
- 数据结构(C#)_排序算法(冒泡排序)
- 数据结构(C#)_排序算法(冒泡排序)
- 数据结构6-排序算法(直接插入排序、希尔排序、快速排序、归并排序和堆排序)
- 数据结构与算法(C#实现)系列---二叉树
- 算法&数据结构(二):查找与排序算法
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 2)
- 数据结构——排序算法总结
- [置顶]C#中使用Redis不同数据结构的内存占有量的疑问和对比测试
- 转:C#数据结构和算法学习系列十二----散列HashTable类
- 数据结构--排序算法(一)冒泡排序
- 数据结构——堆排序和归并排序
- 数据结构与算法——排序算法
- 数据结构——排序算法