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. 【Think In Java笔记】第1章 对象导论

    1. 对象导论 OOP 面向对象编程 C.Basic等语言所在的抽象仍要求在解决问题时基于计算机的解决,而不是基于所解决问题的结构来考虑. 要建立起问题空间的元素和解空间的对象之间一一映射的关系 万物 ...

  2. TensorFlow——MNIST手写数据集

    MNIST数据集介绍 MNIST数据集中包含了各种各样的手写数字图片,数据集的官网是:http://yann.lecun.com/exdb/mnist/index.html,我们可以从这里下载数据集. ...

  3. 为什么双击打开py文件时窗口瞬间关闭了?

    当前理解,py文件里没有input() 等暂停程序运行的函数,程序运行速度太快,运行完就立马关闭了. input()调用后,程序会立即暂停,等待用户输入

  4. Spring Boot2 系列教程(二十) | SpringBoot 是如何实现日志的?

    微信公众号:一个优秀的废人.如有问题,请后台留言,反正我也不会听. 前言 休息日闲着无聊看了下 SpringBoot 中的日志实现,把我的理解跟大家说下. 门面模式 说到日志框架不得不说门面模式.门面 ...

  5. 2次方的期望dp

    某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(    我们来简化一下这个游戏的规则    有n次点击要做,成功了就是o,失败了就是x,分数是按comb计算的,连续a个com ...

  6. svn或git 提交文件排除

    也可以参考  https://blog.csdn.net/chenmintong/article/details/79725324 乌龟git 过滤掉忽略文件(首先右键 某文件 删除并添加到忽略列表 ...

  7. 解决 VS Code 中 golang.org 被墙导致的 Go 插件安装失败问题

    微软官方开发的 Go for Visual Studio Code 插件为 Go 语言 提供了丰富的支持.在 VS Code 中首次打开 Go 工作区后,VS Code 会自动检测当前开发环境为 Go ...

  8. 如何通过Java8的方式去统计程序执行时间?

    代码如下所示 import java.time.Duration; import java.time.Instant; import java.util.concurrent.TimeUnit; pu ...

  9. django操作命令

    下载安装 pip3 install django==1.11.21 -i https://pypi.tuna.tsinghua.edu.cn/simple 创建项目 1.终端找到存放项目的文件夹,dj ...

  10. 深入理解Java虚拟机:JVM高级特性与最佳实践

    第一部分走近Java第1章走近Java21.1概述21.2Java技术体系31.3Java发展史51.4Java虚拟机发展史91.4.1SunClassicExactVM91.4.2SunHotSpo ...