数据结构与算法(C语言版)__归并排序
来源:互联网 发布:云计算操作系统虚拟化 编辑:程序博客网 时间:2024/05/09 22:43
归并排序是对两个已经排序的数组合并在一起。
下面使用迭代的方式实现归并排序
#define _SCL_SECURE_NO_WARNINGS#include<iostream>#include<algorithm>using namespace std;template<class T>void Merge(T *initList, T *mergedList, const int l, const int m, const int n){ int i1, i2, iResult; for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){ if (initList[i1] <= initList[i2]){ mergedList[iResult] = initList[i1]; i1++; } else{ mergedList[iResult] = initList[i2]; i2++; } } copy(initList + i1, initList + m + 1, mergedList + iResult); copy(initList + i2, initList + n + 1, mergedList + iResult);}int main(){ int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 }; int b[11] = { 0 }; Merge(a, b, 1, 4, 10); for (int i = 1; i < 11; ++i){ cout << b[i] << " "; } cout << endl; system("pause"); return 0;}
下面看一步一步进行归并排序
#define _SCL_SECURE_NO_WARNINGS#include<iostream>#include<algorithm>using namespace std;template<class T>void Merge(T *initList, T *mergedList, const int l, const int m, const int n){ int i1, i2, iResult; for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){ if (initList[i1] <= initList[i2]){ mergedList[iResult] = initList[i1]; i1++; } else{ mergedList[iResult] = initList[i2]; i2++; } } copy(initList + i1, initList + m + 1, mergedList + iResult); copy(initList + i2, initList + n + 1, mergedList + iResult);}template<class T>void MergePass(T *initList, T *resultList, const int n, const int s){ int i; for (i = 1; i <= n - 2 * s + 1; i += 2 * s){ Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1); } if ((i + s - 1) < n){ Merge(initList, resultList, i, i + s - 1, n); } else{ copy(initList + i, initList + n + 1, resultList + i); }}int main(){ /*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 }; int b[11] = { 0 }; Merge(a, b, 1, 4, 10); for (int i = 1; i < 11; ++i){ cout << b[i] << " "; } cout << endl;*/ int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 }; int n[11] = { 0 }; MergePass(m, n, 10, 1); for (int i = 1; i < 11; i++) cout << n[i] << " "; cout << endl; MergePass(n, m, 10, 2); for (int i = 1; i < 11; i++) cout << m[i] << " "; cout << endl; MergePass(m, n, 10, 4); for (int i = 1; i < 11; i++) cout << n[i] << " "; cout << endl; MergePass(n, m, 10, 8); for (int i = 1; i < 11; i++) cout << m[i] << " "; cout << endl; system("pause"); return 0;}
下面看整体的归并排序:
#define _SCL_SECURE_NO_WARNINGS#include<iostream>#include<algorithm>using namespace std;template<class T>void Merge(T *initList, T *mergedList, const int l, const int m, const int n){ int i1, i2, iResult; for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){ if (initList[i1] <= initList[i2]){ mergedList[iResult] = initList[i1]; i1++; } else{ mergedList[iResult] = initList[i2]; i2++; } } copy(initList + i1, initList + m + 1, mergedList + iResult); copy(initList + i2, initList + n + 1, mergedList + iResult);}template<class T>void MergePass(T *initList, T *resultList, const int n, const int s){ int i; for (i = 1; i <= n - 2 * s + 1; i += 2 * s){ Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1); } if ((i + s - 1) < n){ Merge(initList, resultList, i, i + s - 1, n); } else{ copy(initList + i, initList + n + 1, resultList + i); }}template<class T>void MergeSort(T *a, const int n){ T *tempList = new int[n + 1]; for (int l = 1; l < n; l *= 2){ MergePass(a, tempList, n, l); l *= 2; MergePass(tempList, a, n, l); } delete[]tempList;//删除临时的数组,防止内存泄露。}int main(){ /*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 }; int b[11] = { 0 }; Merge(a, b, 1, 4, 10); for (int i = 1; i < 11; ++i){ cout << b[i] << " "; } cout << endl;*/ int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 }; int n[11] = { 0 }; MergePass(m, n, 10, 1); for (int i = 1; i < 11; i++) cout << n[i] << " "; cout << endl; MergePass(n, m, 10, 2); for (int i = 1; i < 11; i++) cout << m[i] << " "; cout << endl; MergePass(m, n, 10, 4); for (int i = 1; i < 11; i++) cout << n[i] << " "; cout << endl; MergePass(n, m, 10, 8); for (int i = 1; i < 11; i++) cout << m[i] << " "; cout << endl; cout << "上面是中间数据测试" << endl; cout << "下面是MergeSort...:" << endl; int x[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 }; MergeSort(x, 10); for (int i = 1; i < 11; i++) cout << x[i] << " "; cout << endl; system("pause"); return 0;}
总结:归并排序的速度和快速排序一样,使用三个函数写成一个归并排序,唯一的缺点是需要使用两倍的内存, 如果要排序1M的数据,就要使用2M的内存,因为使用了临时数组。但是现在电脑内存都比较大,所以这个问题也不算严重。
0 0
- 数据结构与算法(C语言版)__归并排序
- 数据结构与算法(C语言版)__冒泡排序
- 数据结构与算法(C语言版)__选择排序
- 数据结构与算法(C语言版)__插入排序
- 数据结构与算法(C语言版)__快速排序
- 数据结构与算法(C语言版)__堆排序
- 数据结构与算法(C语言版)__交换
- 数据结构与算法(C语言版)__顺序查找
- 数据结构与算法(C语言版)__二分查找
- 数据结构与算法(C语言版)__递归
- 数据结构与算法(C语言版)__排列组合
- 数据结构与算法(C语言版)__栈
- 数据结构与算法(C语言版)__队列
- 数据结构与算法(C语言版)__链表
- 数据结构与算法(C语言版)__链表2
- 数据结构与算法(C语言版)__循环链表
- 数据结构与算法(C语言版)__双向链表
- 数据结构与算法(C语言版)__链式栈
- linux yum命令详解
- 科技创新服务平台-科学家发现四种新元素:即将进行命名
- linux应用软件安装包类型及安装介绍
- 几款好用的Tooltips 提示框插件
- 在HTML中加载视频不播放怎么办
- 数据结构与算法(C语言版)__归并排序
- IntellJ Idea工具+Maven 创建Web项目
- android (二) -fragment 的动态加载
- Caffe学习系列(9):运行caffe自带的两个简单例子
- C++构造函数/复制构造函数(超容易理解版)
- C语言-结构体-1887 候选人得票统计
- UML类图常见的几种关系
- EasyUI的引入和使用
- Linux投石问路