归并排序(merging sort) 具体解释 及 代码

本文地址: http://blog.csdn.net/caroline_wendy

归并排序(merging sort): 包括2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表.

归并排序(merge sort)的时间复杂度是O(nlogn), 实际效果不如高速排序(quick sort)和堆排序(heap sort),

可是归并排序是稳定排序, 而高速排序和堆排序则不是.

代码:

/*
* main.cpp
*
* Created on: 2014.6.12
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <algorithm>
#include <iterator> using namespace std; /*參数: SR-输入数组, TR-输出数组, i至m:第一段有序, m+1至n:第二段有序*/
void Merge (const std::vector<int> SR, std::vector<int>& TR, int i, int m, int n)
{
int j , k;
for (j=m+1, k=i; i<=m && j<=n; ++k) {
if (SR[i] < SR[j])
TR[k] = SR[i++];
else
TR[k] = SR[j++];
}
if (i<=m)
std::copy((SR.begin()+i), (SR.begin()+m+1), TR.begin()+k);
if (j<=n)
std::copy((SR.begin()+j), (SR.begin()+n+1), TR.begin()+k);
} /*參数: SR-输入数组, TR-输出数组, s:起始, t:末尾*/
void MSort (const std::vector<int> SR, std::vector<int>& TR, int s, int t)
{
std::vector<int> tempTR(SR.size());
if (s == t)
TR[s] = SR[s];
else {
int m = (s+t)/2; //平分SR, SR[s..m]和SR[m+1..t]
MSort(SR, tempTR, s, m); //前半段
MSort(SR, tempTR, m+1, t); //后半段
Merge(tempTR, TR, s, m, t); //排序
//copy(TR.begin(), TR.end(), ostream_iterator<int>(cout, " "));
//std::cout << std::endl;
}
} void MergeSort (std::vector<int>& L) {
MSort(L, L, 0, L.size()-1);
} int main (void)
{
std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};
MergeSort(L);
copy(L.begin(), L.end(), ostream_iterator<int>(cout, " "));
std::cout << std::endl;
return 0;
}

输出:

13 27 38 49 49 65 76 97

数据结构 - 归并排序(merging sort) 具体解释 及 代码的更多相关文章

  1. 数据结构 - 归并排序(merging sort)

    归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...

  2. 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)

    堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...

  3. 数据结构 - 希尔排序(Shell&#39;s Sort) 具体解释 及 代码(C++)

    数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2 ...

  4. 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)

    树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...

  5. 归并排序(Merging Sort)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. 小小c#算法题 - 8 - 归并排序 (Merging Sort)

    “归并”的含义是将两个或两个以上的有序序列组合成一个新的有序序列.这个“归并”可以在O(n+m)的数量级上实现,但这同时也需要O(n+m)的空间复杂度.具体为:首先分配一个新的长度为n+m的空序列,然 ...

  7. 数据结构 - 表插入排序 具体解释 及 代码(C++)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/24323125 表插入排序 具体解释 及 代码 ...

  8. 数据结构 - 2-路插入排序 具体解释 及 代码(C++)

    2-路插入排序 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24267679 2-路插入排序的思想非常有意思 ...

  9. FZU 1919 -- K-way Merging sort(记忆化搜索)

    题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...

随机推荐

  1. js获取验证码 秒表效果(原创)

    <script src="http://code.jquery.com/jquery-latest.js"></script> <input type ...

  2. k8s traefik ingress tls

    使用下面的 openssl 命令生成 CA 证书: $ openssl req -newkey rsa:2048 -nodes -keyout tls.key -x509 -days 365 -out ...

  3. mod_wsgi 初体验

    1, 安装 ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python3 make && ...

  4. BZOJ 2002 LCT板子题

    思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...

  5. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  6. LeetCode Weekly Contest 21

    1. 530. Minimum Absolute Difference in BST 最小的差一定发生在有序数组的相邻两个数之间,所以对每一个数,找他的前驱和后继,更新结果即可!再仔细一想,bst的中 ...

  7. 利用JavaScript的%读分秒

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 继承&封装

    扩展一个已有的类,并且继承该类的属性和行为这种方式成为继承. 实例 public class Polygon { public int sides; public double area; publi ...

  9. Vs2010无法打开文件“Kernel32.lib”、无法打开“libcpmt.lib”"msvcprt.lib"

    1.对于无法打开"Kernel"问题,即使复制lib文件到目录,仍然会出现最后的错误; 原因:WindowsSdk 安装失败! 方法:重装 microsoft SDK6.0 ,再在 ...

  10. 考考你对java多态的理解

    请看如下代码, 如果你能不运行得出正确答案, 那你真的超过99%的java程序员了. [本人属于最大头的那部分] public class A{ public String s = "A&q ...