归并排序(merge sort)是一个时间复杂度为O(nlogn)的基于比较的排序算法(comparison based sorting algorithm)。 归并排序大多数实现(implementation)都将其实现成了一个stable sort, 所谓的stable sort的意思就是the implementation preserves the input order of equal elements in the sorted order。

merge sort 也采用了divide and conquer(分治技术) 的排序算法。 由John von Neumann于1945年发明的。

我们知道, 快排序的平均情况下时间复杂度是O(nlogn), 最坏情况下为O(n^2)。 归并排序的好处就是时间复杂度总是O(nlogn).。 所以归并排序在时间方面可以beats quick sort。

下面对merge sort做出详细说明:

例如, 我们想要对如下数组精心排序:

merge sort 的思想是将一个problem break into 2 subproblems。

(1)divide the array into 2 equal halves(假如分隔位置在数组的中间(记为mid位置), mid 之前为the first subarray, 之后为the second subarray):

(2) 现在假如我们somehow 将这两个子数组排好序了, 接下来我们的任务就是将这两个子数组merge称为一个大的排好序的数组。 注意这三个数组时独立位于内存之中的(可见merge sort 的空间复杂度会比较大)。

我们的merge算法如下。 现在我们将上图左边的数组记为L, 右边的数组记为R。  首先第一步,比较L的第一个未选中的数据和R中第一个未选中的数据, 找出最小的数据, 为位于L中的1. 我们将这个值放在A中。 然后移动L的索引到下一个元素2, R的索引记录不懂, 比较, 发现2大, 将2 放到A的第二个位置。 在移动L中的索引, 到达4, R的不动, 4 比3大, 所以将3 放在A的第三个位置, 移动R的索引记录到下一个元素。 以此类推, 直至R(或者L)的元素均位于A中, 然后将L(或者R)剩余的元素均拷贝到A中。 即完成排序。

上述算法的伪代码如下:

现在我们说说如何排序。

解决办法是我们可以进一步的将数组或者list 进行进一步的细分:

到达最后一步的时候, 然后开始从下网上归并(merge)。

最终结果如下:

归并排序的伪代码如下:

程序如下:

/* Merge sort in C++ */
#include <cstdio>
#include <iostream>using namespace std;// Function to Merge Arrays L and R into A.
// lefCount = number of elements in L
// rightCount = number of elements in R.
void Merge(int *A,int *L,int leftCount,int *R,int rightCount) {int i,j,k;// i - to mark the index of left aubarray (L)// j - to mark the index of right sub-raay (R)// k - to mark the index of merged subarray (A)i = 0; j = 0; k =0;while(i<leftCount && j< rightCount) {if(L[i]  < R[j]) A[k++] = L[i++];else A[k++] = R[j++];}while(i < leftCount) A[k++] = L[i++];while(j < rightCount) A[k++] = R[j++];
}// Recursive function to sort an array of integers.
void MergeSort(int *A,int n) {int mid,i, *L, *R;if(n < 2) return; // base condition. If the array has less than two element, do nothing.mid = n/2;  // find the mid index.// create left and right subarrays// mid elements (from index 0 till mid-1) should be part of left sub-array// and (n-mid) elements (from mid to n-1) will be part of right sub-arrayL = new int[mid];R = new int [n - mid];for(i = 0;i<mid;i++) L[i] = A[i]; // creating left subarrayfor(i = mid;i<n;i++) R[i-mid] = A[i]; // creating right subarrayMergeSort(L,mid);  // sorting the left subarrayMergeSort(R,n-mid);  // sorting the right subarrayMerge(A,L,mid,R,n-mid);  // Merging L and R into A as sorted list.// the delete operations is very importantdelete [] R;delete [] L;
}int main() {/* Code to test the MergeSort function. */int A[] = {6,2,3,1,9,10,15,13,12,17}; // creating an array of integers.int i,numberOfElements;// finding number of elements in array as size of complete array in bytes divided by size of integer in bytes.// This won't work if array is passed to the function because array// is always passed by reference through a pointer. So sizeOf function will give size of pointer and not the array.// Watch this video to understand this concept - http://www.youtube.com/watch?v=CpjVucvAc3gnumberOfElements = sizeof(A)/sizeof(A[0]);// Calling merge sort to sort the array.MergeSort(A,numberOfElements);//printing all elements in the array once its sorted.for(i = 0;i < numberOfElements;i++)cout << " " << A[i];return 0;
}

运行结果为:

NOTE: 在code::blocks 下可以调试, 单步运行, 打开watches 窗口查看各个变量

也可以查看call stack:

C++ Merge sort(归并排序)相关推荐

  1. C语言merge sort归并排序算法(附完整源码)

    C语言merge sort归并排序算法 merge sort归并排序算法的完整源码(定义,实现) merge sort归并排序算法的完整源码(定义,实现) #ifndef MERGE_SORT_H # ...

  2. 经典排序算法(五) —— Merge Sort 归并排序

    文章目录 简介 排序过程 实现 版本一 版本二 复杂度 简介 约翰·冯·诺伊曼在 1945 年提出了归并排序.归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and ...

  3. C语言归并排序Merge Sort算法(附完整源码)

    归并排序Merge Sort 归并排序Merge Sort算法的完整源码(定义,实现,main函数测试) 归并排序Merge Sort算法的完整源码(定义,实现,main函数测试) #include ...

  4. python代码实现归并排序(Merge Sort )

    python代码实现归并排序(Merge Sort ) 归并排序(Merge Sort) 归并排序,是创建在归并操作上的一种有效的排序算法.算法是采用分治法(Divide and Conquer)的一 ...

  5. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  6. C语言以递归实现归并排序Merge Sort算法(附完整源码)

    以递归实现归并排序Merge Sort 以递归实现归并排序Merge Sort算法的完整源码(定义,实现,main函数测试) 以递归实现归并排序Merge Sort算法的完整源码(定义,实现,main ...

  7. 排序算法二:归并排序(Merge sort)

    归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 1 private void merge_sort(int[] array, i ...

  8. python选择排序算法图解_python基本算法之实现归并排序(Merge sort)

    0.前言 评判一个算法的好坏的标准: 时间复杂度 空间复杂度 1.归并排序算法是什么? 冒泡排序(Bubble Sort)是一种建立在归并操作上面的一种有效的排序算法,由John von neuman ...

  9. python 归并排序算法_python基本算法之实现归并排序(Merge sort)

    0.前言 评判一个算法的好坏的标准: 时间复杂度 空间复杂度 1.归并排序算法是什么? 冒泡排序(Bubble Sort)是一种建立在归并操作上面的一种有效的排序算法,由John von neuman ...

  10. 归并排序(merge sort)算法实现

    归并排序(merge sort)体现了分治的思想,即将一个待排序数组分为两部分,对这两个部分进行归并排序,排序后,再对两个已经排序好的数组进行合并.这种思想可以用递归方式很容易实现.归并排序的时间复杂 ...

最新文章

  1. hive无法执行带where语句的SQL
  2. 如何设置网件gs108e_硬核评测!Wi-Fi 6 到底有多快,如何更快?
  3. win8/Metro开发系七 win8 对常见数据源的解析及处理 如:xml,json,以及html代码
  4. PHP----------安装包lnmp1.3-full安装的lnmp环境,如何安装PHP扩展
  5. Linux 和 Vim 常用命令整理
  6. 系统安全防护之UNIX下***检测方法
  7. 【NOI2011】兔兔与蛋蛋的游戏【二分图博弈】
  8. 前端学习(3058):vue+element今日头条管理-回顾
  9. 转:基于TLS1.3的微信安全通信协议mmtls介绍
  10. 使用Tensorflow Lite创建一个Android AI应用
  11. html尾部代码_3分钟短文:Laravel Form,让你不再写 HTML 的好“库”
  12. 拉丁超立方试验设计_南水北调工程通水 六年惠及超1.2亿人
  13. 多个项目共用同一个redis_分区:如何在多个Redis实例之间拆分数据
  14. [论文阅读] TransFuse: Fusing Transformers and CNNs for Medical Image Segmentation
  15. 日志的打印 —— Java 支持
  16. DOA——MUSIC算法
  17. CENTOS利用Keepalived构建双主MySQL+双机热备
  18. c语言结构体简单试题,C语言6结构体练习题6
  19. 2021桂林市高考一调成绩查询,2021年广西桂林市高考化学一调试卷.docx
  20. 我开着车 你带着我

热门文章

  1. [Java] 使用HttpClient实现文件下载
  2. 递归算法实例:阶乘(vb.net代码)
  3. VMware下载(官网)
  4. 你安全吗网剧技术探讨-个人向
  5. 计算机服务中无spool,打印服务SPOOLSV.EXE自动停止
  6. UOS系统应用商店提示安装失败
  7. docker安装tomcat8
  8. 兄弟打印机内存已满清零方法_兄弟打印机清零大全
  9. 远控免杀从入门到实践 (11) 终结篇
  10. 微信小程序 - 快速搭建微信小程序demo