Algorithms - Merging Sort
印象

图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;
}
推荐
参考
Algorithms - Merging Sort的更多相关文章
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
- FZU 1919 -- K-way Merging sort(记忆化搜索)
题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...
- 归并排序(Merging Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 数据结构 - 归并排序(merging sort) 具体解释 及 代码
归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- [Algorithms] Radix Sort
Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Simplest Python K-Way Merging Sort|最简单的Python k路归并排序
想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...
- [Algorithms] Counting Sort
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...
随机推荐
- PCBA 生产需要什么文件? (2018-07-10)
PCBA 生产需要什么文件? 生产需要资料 工单套料单 生产说明文件 生产贴片图 正面含元件号 背面含元件号 钢网资料(可以是 Gerber) 元件坐标图
- 运行flask程序
Command Line Interface Installing Flask installs the flask script, a Click command line interface, i ...
- Extjs5.0 学习之路【结构篇】
Extjs5.0在原有的MVC模式下增加了一个MVVM Extjs5.0项目文件执行顺序. 新增特性一 bind---data
- Windows 7 下将 Tomcat Java 程序设置为 Windows Service
方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...
- c# 使用GetPrivateProfileString 读ini数据 失败
项目中用到 GetPrivateProfileString但是使用中, 发现 无法读出 ini 配置中的值, 比如Enable_log =3 我读到的是 API设置的默认值. 网上说可能时字符集编码的 ...
- 手机定位室内gps没信号
手机定位一般分3种,gps,手机信号基站,上网地点,其中gps信号一般只有户外有,所以在室外的时候只开启定位和gps就可以定位了,但是在室内没有gps的情况,就需要开网络定位了.
- windows下python访问ipv6报错
错误 Traceback (most recent call last): File , in <module> app.run() File , in run return wsgi.r ...
- 重新认识synchronized(上)
synchronized在JDK5之前一直被称为重量级锁,是一个较为鸡肋的设计,而在JDK6对synchronized内在机制进行了大量显著的优化,加入了CAS,轻量级锁和偏向锁的功能,性能上已经跟R ...
- PowerDesigner的CDM模型将低驼峰命名法则的每个大写字母前加_符
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- Android 4学习(2):概述 - AndroidManifes.xml
Android应用程序包含下面这几个部分: 程序的前后台: Activities Services 存储: Content Providers 消息传递: Intents Broadcast Rece ...