1. The divide and conquer approach - 归并排序
  2. 归并排序所应用的理论思想叫做分治法.
  3. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题,
  4. 然后递归(recursive) 求解这些子问题, 最后再合并这些子问题的解以求得
  5. 原问题的解.
  6. 即, 分解 -> 解决 -> 合并.
  7.  
  8. The divide and conquer approach
  9. 分解: 将待排序的含有 n 个元素的的序列分解成两个具有 n/2 的两个子序列.
  10. 解决: 使用归并排序递归地排序两个子序列.
  11. 合并: 合并两个已排序的子序列得出结果.
  12.  
  13. 归并排序算法的 '时间复杂度' nlogn
  14.  
  15. import time, random
  16.  
  17. def sortDivide(alist): # 分解 divide
  18. if len(alist) <= 1:
  19. return alist
  20. l1 = sortDivide(alist[:alist.__len__()//2])
  21. l2 = sortDivide(alist[alist.__len__()//2:])
  22. return sortMerge(l1,l2)
  23.  
  24. def sortMerge(l1, l2): # 解决 & 合并 sort & merge
  25. listS = []
  26. print("Left - ", l1)
  27. print("Right - ", l2)
  28. i,j = 0,0
  29. while i < l1.__len__() and j < l2.__len__():
  30. if l1[i] <= l2[j]:
  31. listS.append(l1[i])
  32. i += 1
  33. print("-i", i)
  34. else:
  35. listS.append(l2[j])
  36. j += 1
  37. print("-j", j)
  38. print(listS)
  39. else:
  40. if i == l1.__len__():
  41. listS.extend(l2[j:])
  42. else:
  43. listS.extend(l1[i:])
  44. print(listS)
  45. print("Product -",listS)
  46. return listS
  47.  
  48. def randomList(n,r):
  49. F = 0
  50. rlist = []
  51. while F < n:
  52. F += 1
  53. rlist.append(random.randrange(0,r))
  54. return rlist
  55.  
  56. if __name__ == "__main__":
  57. alist = randomList(9,100)
  58. print("List-O",alist)
  59. startT =time.time()
  60. print("List-S", sortDivide(alist))
  61. endT = time.time()
  62. print("Time elapsed :", endT - startT)
  63.  
  64. output,
  65. List-O [88, 79, 52, 78, 0, 43, 21, 55, 62]
  66. Left - [88]
  67. Right - [79]
  68. -j 1
  69. [79]
  70. [79, 88]
  71. Product - [79, 88]
  72. Left - [52]
  73. Right - [78]
  74. -i 1
  75. [52]
  76. [52, 78]
  77. Product - [52, 78]
  78. Left - [79, 88]
  79. Right - [52, 78]
  80. -j 1
  81. [52]
  82. -j 2
  83. [52, 78]
  84. [52, 78, 79, 88]
  85. Product - [52, 78, 79, 88]
  86. Left - [0]
  87. Right - [43]
  88. -i 1
  89. [0]
  90. [0, 43]
  91. Product - [0, 43]
  92. Left - [55]
  93. Right - [62]
  94. -i 1
  95. [55]
  96. [55, 62]
  97. Product - [55, 62]
  98. Left - [21]
  99. Right - [55, 62]
  100. -i 1
  101. [21]
  102. [21, 55, 62]
  103. Product - [21, 55, 62]
  104. Left - [0, 43]
  105. Right - [21, 55, 62]
  106. -i 1
  107. [0]
  108. -j 1
  109. [0, 21]
  110. -i 2
  111. [0, 21, 43]
  112. [0, 21, 43, 55, 62]
  113. Product - [0, 21, 43, 55, 62]
  114. Left - [52, 78, 79, 88]
  115. Right - [0, 21, 43, 55, 62]
  116. -j 1
  117. [0]
  118. -j 2
  119. [0, 21]
  120. -j 3
  121. [0, 21, 43]
  122. -i 1
  123. [0, 21, 43, 52]
  124. -j 4
  125. [0, 21, 43, 52, 55]
  126. -j 5
  127. [0, 21, 43, 52, 55, 62]
  128. [0, 21, 43, 52, 55, 62, 78, 79, 88]
  129. Product - [0, 21, 43, 52, 55, 62, 78, 79, 88]
  130. List-S [0, 21, 43, 52, 55, 62, 78, 79, 88]
  131. 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. echarts更改坐标轴文字颜色及大小

    xAxis: { data: anameArr, axisLabel: { show: true, textStyle: { color: '#c3dbff', //更改坐标轴文字颜色 fontSiz ...

  2. 怎么将文件夹上传到GitHub上

    1. 在GitHub上新建一个仓库地址: http://github.com/......git 2. 在需要上传的文件夹目录下,运行 git   init  初始化git: 3. 运行git  ad ...

  3. 256位AES加密和解密

    /// <summary> /// 256位AES加密 /// </summary> /// <param name="toEncrypt">& ...

  4. crawler 听课笔记 碎碎念 2 一些爬虫须知的基本常识和流程

    html的宗旨:      <标签 属性=”属性的值“></标签>        只是对于文本的一种解释划分吧 dom的宗旨:      就是一个大数组,处理方便,效率低 xm ...

  5. Activiti 规则任务(businessRuleTask)

    Activiti 规则任务(businessRuleTask) 作者:Jesai 目前国内研究Activiti规则任务businessRuleTask)的文章在网上应该不超出3篇 小觑夜漫酒作伴,破晓 ...

  6. 2019CSP复赛游记

    Day 0 作为一个初三的小蒟蒻…… 什么算法都不会打…… 做一道LCA+生成树的图论题调了两个小时…… 明日裸考…… Day 1 Morning 买了两个士力架,带了一盒牛奶,准备在考场上食用(这个 ...

  7. hge引擎使用技巧

    图片周围最好留出一像素,即上下左右都多出一像素.然后使用pngopt.exe处理一下.这样可以减少图片拉伸.旋转时边界模糊的情况 图片宽高最好是 2的N次方

  8. Python3 正则表达式 re 模块的使用 - 学习笔记

    re 模块的引入 re 模块的使用 re.compile() re.match()与re.search() re.match re.search() 区别 re.findall()与re.findit ...

  9. MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作

    MyBatis-Plus是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,使用MyBatis-Plus时,不会影响原来Mybatis方式的使用. SpringBoot+M ...

  10. php--->单例模式封装mysql操作类

    php 单例模式封装mysql操作类 单例模式的必要条件(三私一公) 私有的成员属性--防止类外引入这个存放对象的属性 私有的构造方法--为了防止在类外使用new关键字实例化对象 私有的克隆方法--为 ...