再次理解 归并排序
来源:互联网 发布:云计算ai工程师 编辑:程序博客网 时间:2024/06/08 13:20
归并排序:先对两个有序的系列进行合并,合并的时候不断的对两个系列的第一个元素进行比较,把较小的那个移动到最前面成为了第一个元素,那么移动的元素后面的元素就是成为了下次比较的序列的第一个元素,如此不断的取两个系列的第一个元素进行比较。
1 4 5 6 2 7 8 9 第一轮1与2比较 1比2小, 那么1被移动了 4成为了下次要比较的元素了
那么下一轮就是比较4和2 2小就被移动了 那么再次比较的就是4和7了 如此一轮一轮的比较。
- //merge two array:对两个有序列进行合并
- void merge(int a[], int temp[], int first, int mid, int end)
- {
- int i = first, j = mid + 1;
- int m = mid, n = end;
- int k = 0;
- while (i <= m && j <= n)
- {
- if (a[i] <= a[j])
- temp[k++] = a[i++];
- else
- temp[k++] = a[j++];
- }
- while (i <= m)
- temp[k++] = a[i++];
- while (j <= n)
- temp[k++] = a[j++];
- for (int i = 0; i < k; i++)/*把存储在临时对象temp中排好序的元素copy到a中对应的下标上*/
- a[first + i] = temp[i];
- }
//merge two array:对两个有序列进行合并void merge(int a[], int temp[], int first, int mid, int end){ int i = first, j = mid + 1; int m = mid, n = end; int k = 0; while (i <= m && j <= n) { if (a[i] <= a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while (i <= m) temp[k++] = a[i++]; while (j <= n) temp[k++] = a[j++]; for (int i = 0; i < k; i++)/*把存储在临时对象temp中排好序的元素copy到a中对应的下标上*/ a[first + i] = temp[i];}
上面只是对两个有序的系列进行合并,还没有对序列进行排序,那么如何排序了,这里我们要用到递归的思想,就是把序列分成若干个系列再合并,分裂到最后必然小的系列,包含一个元素,一个元素的系列必然是有序的, 然后逆向合并,合并起来的必然是有序的。
- void merge_sort(int a[], int first, int last, int temp[])
- {
- if (first < last)
- {
- int mid = (first + last) / 2;
- merge_sort(a, first, mid, temp); //it’s let letf have a regular
- merge_sort(a, mid + 1, last, temp); //it’s let right have a regular
- merge(a, temp, first, mid, last); //merge two in one
- }
- }
void merge_sort(int a[], int first, int last, int temp[]){ if (first < last) { int mid = (first + last) / 2; merge_sort(a, first, mid, temp); //it's let letf have a regular merge_sort(a, mid + 1, last, temp); //it's let right have a regular merge(a, temp, first, mid, last); //merge two in one }}
写个测试代码:
- int main()
- {
- int a[] = { 1, 8, 6, 7, 9, 45, 68, 100, 5 };
- int k = (sizeof(a) / sizeof(int));
- int *p = new int[k];
- merge_sort(a,0,k-1,p);
- for (int i = 0; i < k; i++)
- cout << a[i] << ” ”;
- return 0;
- }
int main(){ int a[] = { 1, 8, 6, 7, 9, 45, 68, 100, 5 }; int k = (sizeof(a) / sizeof(int)); int *p = new int[k]; merge_sort(a,0,k-1,p); for (int i = 0; i < k; i++) cout << a[i] << " "; return 0;}
以上内容纯属转载。这次画了解答树,本人对归并排序的理解又进一步加深了。参考:http://blog.csdn.net/chenxun_2010/article/details/40717475
阅读全文
0 0
- 再次理解 归并排序
- 归并排序理解
- 归并排序理解
- 归并排序的若干理解
- 归并排序算法的理解
- 归并排序的理解记录
- 归并排序算法个人理解
- 理解快速排序和归并排序
- 堆排序以及归并排序的理解
- 对比快速排序,理解归并排序
- 我所理解的归并排序算法
- 关于对于归并排序的理解
- 算法排序 归并 理解性写
- 我对归并排序的理解
- “深入理解”—归并排序算法
- 归并排序-归并排序
- 深入浅出理解排序算法之-归并排序
- 排序算法之归并排序的理解与实现
- WOJ1222-Cheapest Palindrome
- 关于Collections的学习笔记
- vsftpd虚拟用户设置
- eclipse工作空间的基本配置
- Java_基础—网络编程(TCP协议代码及其优化和服务器多线程优化)
- 再次理解 归并排序
- Android Bitmap与String的相互转换
- react开发笔记 —ant-design框架
- Android Camera 流程学习记录(四)—— Camera.startPreview() flow
- 数据库锁表的分析与解决(二)
- A Comprehensive Guide to Data Exploration(数据探索的综合指南)
- js变量赋值3e3
- 数据库基本概念&MySQL基本操作详解
- c++ 输出到文本格式 ofstream && FILE