印象

图1 使用合并排序为一列数字进行排序的过程

思想

归并排序是典型的分治算法,它不断地将某个数组分为两个部分,分别对左子数组与右子数组进行排序,然后将两个数组合并为新的有序数组。

分析

  • 稳定: 是
  • 时间复杂度:
    • 最优时间: O(nlog(n))
    • 最坏时间: O(nlog(n))
    • 平均时间: O(nlog(n))

实例代码

C#

public static List<int> sort(List<int> lst) {
if (lst.Count <= 1)
return lst;
int mid = lst.Count / 2;
List<int> left = new List<int>(); // 定义左侧List
List<int> right = new List<int>(); // 定义右侧List
// 以下兩個循環把 lst 分為左右兩個 List
for (int i = 0; i < mid; i++)
left.Add(lst[i]);
for (int j = mid; j < lst.Count; j++)
right.Add(lst[j]);
left = sort(left);
right = sort(right);
return merge(left, right);
}
/// <summary>
/// 合併兩個已經排好序的List
/// </summary>
/// <param name="left">左側List</param>
/// <param name="right">右側List</param>
/// <returns></returns>
static List<int> merge(List<int> left, List<int> right) {
List<int> temp = new List<int>();
while (left.Count > 0 && right.Count > 0) {
if (left[0] <= right[0]) {
temp.Add(left[0]);
left.RemoveAt(0);
} else {
temp.Add(right[0]);
right.RemoveAt(0);
}
}
if (left.Count > 0) {
for (int i = 0; i < left.Count; i++)
temp.Add(left[i]);
}
if (right.Count > 0) {
for (int i = 0; i < right.Count; i++)
temp.Add(right[i]);
}
return temp;
}

推荐

博客园 - 图解排序算法(四)之归并排序

参考

Wikipedia - Merge sort

Algorithms - Merging Sort的更多相关文章

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

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

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

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

  3. 归并排序(Merging Sort)

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

  4. 数据结构 - 归并排序(merging sort) 具体解释 及 代码

    归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...

  5. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  6. [Algorithms] Radix Sort

    Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the ...

  7. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  8. Simplest Python K-Way Merging Sort|最简单的Python k路归并排序

    想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...

  9. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...

随机推荐

  1. WebSphere & Log4j

    IBM的东西,真是太麻烦了,一个日志都给你替换掉,太霸道了,Google了下,居然是个普遍想象,不过也有相关的解决方 案,基本好像都是在 WAS5,WAS6.1上的,我没环境,不过我这边的WAS7 没 ...

  2. DIV设置浮动float以后下一个DIV要换行的方法

    <div style=“float:left;”> 1111111 </div> <div style=“float:left;”>222222 </div& ...

  3. [转]实现微信浏览器内打开App Store链接

    微信浏览器是不支持打开App Store 页面的,不知道微信为什么这么做.比如你页面写 <a href=”http://itunes.apple.com/us/app/id399608199″& ...

  4. k近邻算法C++二维情况下的实现

    k近邻算法C++二维实现 这是一个k近邻算法的二维实现(即K=2的情况). #include <cstdio> #include <cstring> #include < ...

  5. 蓝桥杯 基础练习 BASIC-14 时间转换

    基础练习 时间转换   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个以秒为单位的时间t,要求用“<H>:<M>:<S>”的格式来表示这个时间 ...

  6. 关于mybatis中基本类型条件判断问题

    零:sql动态语句中经常会有根据数据库某个字段状态进行判断的 如:status=0为未激活,status=1为激活的,那搜索未激活时: <if test="model.activeSt ...

  7. dirs命令

    dirs命令显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录).dirs始终显示当然目录, 再是堆栈中的内容:即使目录堆栈为空, dirs命令仍然只显示当然目录. 语法 dir ...

  8. git 学习 多个提交用同一个commit

    git add .git commit --amend(连续按连个ZZ)git push -f origin ibm_branch(命令行可能不好用,用IDEA force push好用)

  9. Ubuntu 下 ROS Kinetic 的安装

    安装环境为 Ubuntu 16.04 配置 Ubuntu 软件仓库 打开“设置”中的“软件和更新” 把 “restricted”.“universe” 和 “multiverse” 这三项勾上 勾完后 ...

  10. css3弹性布局语法全解

    本文介绍css3弹性布局的语法 html布局 <div class="box"> <div class="item">1</div ...