The divide and conquer approach - 归并排序
归并排序所应用的理论思想叫做分治法.
分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题,
然后递归(recursive) 求解这些子问题, 最后再合并这些子问题的解以求得
原问题的解.
即, 分解 -> 解决 -> 合并. The divide and conquer approach
分解: 将待排序的含有 n 个元素的的序列分解成两个具有 n/2 的两个子序列.
解决: 使用归并排序递归地排序两个子序列.
合并: 合并两个已排序的子序列得出结果. 归并排序算法的 '时间复杂度' 是 nlogn import time, random def sortDivide(alist): # 分解 divide
if len(alist) <= 1:
return alist
l1 = sortDivide(alist[:alist.__len__()//2])
l2 = sortDivide(alist[alist.__len__()//2:])
return sortMerge(l1,l2) def sortMerge(l1, l2): # 解决 & 合并 sort & merge
listS = []
print("Left - ", l1)
print("Right - ", l2)
i,j = 0,0
while i < l1.__len__() and j < l2.__len__():
if l1[i] <= l2[j]:
listS.append(l1[i])
i += 1
print("-i", i)
else:
listS.append(l2[j])
j += 1
print("-j", j)
print(listS)
else:
if i == l1.__len__():
listS.extend(l2[j:])
else:
listS.extend(l1[i:])
print(listS)
print("Product -",listS)
return listS def randomList(n,r):
F = 0
rlist = []
while F < n:
F += 1
rlist.append(random.randrange(0,r))
return rlist if __name__ == "__main__":
alist = randomList(9,100)
print("List-O",alist)
startT =time.time()
print("List-S", sortDivide(alist))
endT = time.time()
print("Time elapsed :", endT - startT) output,
List-O [88, 79, 52, 78, 0, 43, 21, 55, 62]
Left - [88]
Right - [79]
-j 1
[79]
[79, 88]
Product - [79, 88]
Left - [52]
Right - [78]
-i 1
[52]
[52, 78]
Product - [52, 78]
Left - [79, 88]
Right - [52, 78]
-j 1
[52]
-j 2
[52, 78]
[52, 78, 79, 88]
Product - [52, 78, 79, 88]
Left - [0]
Right - [43]
-i 1
[0]
[0, 43]
Product - [0, 43]
Left - [55]
Right - [62]
-i 1
[55]
[55, 62]
Product - [55, 62]
Left - [21]
Right - [55, 62]
-i 1
[21]
[21, 55, 62]
Product - [21, 55, 62]
Left - [0, 43]
Right - [21, 55, 62]
-i 1
[0]
-j 1
[0, 21]
-i 2
[0, 21, 43]
[0, 21, 43, 55, 62]
Product - [0, 21, 43, 55, 62]
Left - [52, 78, 79, 88]
Right - [0, 21, 43, 55, 62]
-j 1
[0]
-j 2
[0, 21]
-j 3
[0, 21, 43]
-i 1
[0, 21, 43, 52]
-j 4
[0, 21, 43, 52, 55]
-j 5
[0, 21, 43, 52, 55, 62]
[0, 21, 43, 52, 55, 62, 78, 79, 88]
Product - [0, 21, 43, 52, 55, 62, 78, 79, 88]
List-S [0, 21, 43, 52, 55, 62, 78, 79, 88]
Time elapsed : 0.0010027885437011719

The Divide and Conquer Approach - 归并排序的更多相关文章

  1. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  2. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  3. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  4. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  6. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  7. 分治法 - Divide and Conquer

    在计算机科学中,分治法是一种很重要的算法.分治法即『分而治之』,把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的 ...

  8. [算法]分治算法(Divide and Conquer)

    转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...

  9. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

随机推荐

  1. mongodb学习(二)——基本的数据操作

    数据操作(重点) 数据库的核心--CRUD,增加和删除较为简单,查询和修改较复杂 查询 关系运算符 $gt 大于 $lt 小于 $gte 大于等于 $lte 小于等于 $eq | (key: valu ...

  2. wannafly camp day4

    2088: 电音之王 描述 题目描述: 终于活成了自己讨厌的样子. 听说多听电音能加快程序运行的速度. 定义一个数列,告诉你a0,a1,m0,m1,ca\_0,a\_1,m\_0,m\_1,ca0​, ...

  3. poj 2253 最短路 or 最小生成树

    Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...

  4. windows服务搭建(VS2019创建Windows服务不显示安装组件)

    1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer  using System.Timers; 如上图,按tab键快速操作  会自动创建一个委托 改为下边的方式,打印日志来记录 ...

  5. html包含html文件的方法

    我们在写asp页面的时候,常常使用include命令来包含公共文件.由于这个方法用起来非常方便,于是很多人在HTML页面里尝试使用include,但是发现根本就不起作用.这是因为,include是VB ...

  6. 使用RobotFramework的DataBaseLibrary(Java实现)

    RobotFramework能用Python和Jython两条腿走路.但有的时候你得选一条.今天就碰上个问题,为了整合其它模块必须用Java实现的DataBaseLibrary 其实实它很简单,记录步 ...

  7. 异数OS 2017 DPDK 峰会观后感

    1.DPDK in Container 使用虚拟网卡设备技术为每一个容器分配一个IP 网卡适配器(queue).容器技术可以解决虚拟机技术中虚拟机过于臃肿,难于热迁移的问题,可能可以代替美团OVS方案 ...

  8. python super()函数:调用父类的构造方法

    python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...

  9. 创建自定义的RouteBase实现(Creating a Custom RouteBase Implementation) |定制路由系统 |

  10. airtest启用本地python环境的方法

    实现目标,air如果想引用第三方python库,则需要在本地python欢迎执行运行 1.打开设置,红色箭头处,选择本地python路径 2.安装air的两个核心库airtest和pocoui 安装方 ...