惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
Security Latest
Security Latest
S
Schneier on Security
T
Threat Research - Cisco Blogs
Latest news
Latest news
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Blog — PlanetScale
Blog — PlanetScale
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
月光博客
月光博客
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
L
LINUX DO - 最新话题
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
www.infosecurity-magazine.com
www.infosecurity-magazine.com
aimingoo的专栏
aimingoo的专栏
D
Docker
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LangChain Blog

博客园 - fengyv

塑造职场影响力的五大法宝 怎样培养独挡一面的能力 [分享]恼人的设计模式 Git使用总结 设计师整理的系统开发流程-简洁又有重点 JavaScript中的String对象 python高效解析日志入库 如何让js不产生冲突,避免全局变量的泛滥,合理运用命名空间 HTML CSS——margin和padding的学习 三层浅析及示例分析 C语言的代码内存布局详解 超级立方体小记 如何和项目经理沟通产品的交付? CentOS配置smaba与Windows共享文件 Javascript实现简单的下拉二级菜单 从测试员到测试负责人 项目团队中4种组员类型的相应管理方式 在软件项目管理中如何把时间估算的靠近真实值? 性能优化——算法优化
数据结构 - 归并排序(merging sort)
fengyv · 2014-06-17 · via 博客园 - fengyv

Posted on 2014-06-17 21:18  fengyv  阅读(542)  评论()    收藏  举报

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

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

但是归并排序是稳定排序, 而快速排序和堆排序则不是.

代码:

  1.   
  2.   
  3. #include <iostream>  
  4. #include <algorithm>  
  5. #include <iterator>  
  6.   
  7. using namespace std;  
  8.   
  9. void Merge (const std::vector<int> SR, std::vector<int>& TR, int i, int m, int n)  
  10. {  
  11.     int j , k;  
  12.     for (j=m+1, k=i; i<=m && j<=n; ++k) {  
  13.         if (SR[i] < SR[j])  
  14.             TR[k] = SR[i++];  
  15.         else  
  16.             TR[k] = SR[j++];  
  17.     }  
  18.     if (i<=m)  
  19.         std::copy((SR.begin()+i), (SR.begin()+m+1), TR.begin()+k);  
  20.     if (j<=n)  
  21.         std::copy((SR.begin()+j), (SR.begin()+n+1), TR.begin()+k);  
  22. }  
  23.   
  24. void MSort (const std::vector<int> SR, std::vector<int>& TR, int s, int t)  
  25. {  
  26.     std::vector<int> tempTR(SR.size());  
  27.     if (s == t)  
  28.         TR[s] = SR[s];  
  29.     else {  
  30.         int m = (s+t)/2; 
  31.         MSort(SR, tempTR, s, m); 
  32.         MSort(SR, tempTR, m+1, t); 
  33.         Merge(tempTR, TR, s, m, t); 
  34.         
  35.         
  36.     }  
  37. }  
  38.   
  39. void MergeSort (std::vector<int>& L) {  
  40.     MSort(L, L, 0, L.size()-1);  
  41. }  
  42.   
  43. int main (void)  
  44. {  
  45.     std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};  
  46.     MergeSort(L);  
  47.     copy(L.begin(), L.end(), ostream_iterator<int>(cout, " "));  
  48.     std::cout << std::endl;  
  49.     return 0;  
  50. }  


输出:

    1. 13 27 38 49 49 65 76 97