algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1rem; line-height: 1.42857143; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); overflow-x: hidden; }a:active, a:hover { outline: 0px; }::selection { background-color: rgb(181, 214, 252); text-shadow: none; background-position: initial initial; background-repeat: initial initial; }#write { max-width: 854px; margin: 0px auto; height: auto; width: inherit; word-break: normal; word-wrap: break-word; position: relative; white-space: pre-wrap; text-align: justify; padding-bottom: 70px; }body.typora-export { padding-left: 30px; padding-right: 30px; }.typora-export #write { margin: 0px auto; }#write > p:first-child, #write > ul:first-child, #write > pre:first-child, #write > blockquote:first-child, #write > div:first-child { margin-top: 30px; }img { max-width: 100%; }input, button, select, textarea { color: inherit; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; }input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }::before, ::after, * { box-sizing: border-box; }#write p, #write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write div, #write pre { width: inherit; }h1 { font-size: 2rem; }p, .mathjax-block { display: block; -webkit-margin-before: 1rem; -webkit-margin-after: 1rem; -webkit-margin-start: 0px; -webkit-margin-end: 0px; }.hidden { display: none; }.md-blockmeta { color: rgb(204, 204, 204); font-weight: bold; font-style: italic; }a { cursor: pointer; }li span { min-width: 10px; }#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; margin: 4px 0px 0px; }tr { page-break-inside: avoid; page-break-after: auto; }thead { display: table-header-group; }table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; page-break-inside: auto; }table.md-table td { min-width: 80px; }.CodeMirror-placeholder { opacity: 0.3; }.CodeMirror-code pre { padding: 0px; }.CodeMirror-lines { padding: 0px; }div.hr:focus { cursor: none; }.md-fences, pre.md-fences { font-size: 0.9rem; display: block; page-break-inside: avoid; text-align: left; overflow: visible; white-space: pre; position: relative !important; }.md-fences .CodeMirror.cm-s-default.CodeMirror-wrap { top: -1.6em; margin-bottom: -1.6em; }.md-fences.mock-cm { white-space: pre-wrap; }.footnotes { color: rgb(136, 136, 136); font-size: 0.9rem; padding-top: 1em; padding-bottom: 1em; }.footnotes + .footnotes { margin-top: -1em; }sub, sup { line-height: inherit; position: inherit; top: inherit; vertical-align: super; }.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background-color: transparent; text-decoration: none; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1rem; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; line-height: normal; font-weight: normal; text-align: left; box-sizing: content-box; direction: ltr; background-position: initial initial; background-repeat: initial initial; }li div { padding-top: 0px; }blockquote { margin: 1rem 0px; }li p, li .mathjax-block { margin: 0.5rem 0px; }li { margin: 0px; position: relative; }blockquote > :last-child { margin-bottom: 0px; }blockquote > :first-child { margin-top: 0px; }.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; }@media print { html, body { height: 100%; } .typora-export * { -webkit-print-color-adjust: exact; }}.footnote-line { margin-top: 0.714em; font-size: 0.7em; }a img, img a { cursor: pointer; }#write pre.md-meta-block { font-size: 0.8rem; min-height: 2.86rem; white-space: pre; background-color: rgb(204, 204, 204); display: block; background-position: initial initial; background-repeat: initial initial; }p > .md-image:only-child { display: inline-block; width: 100%; text-align: center; }#write .MathJax_Display { margin: 0.8em 0px 0px; }.mathjax-block { white-space: pre; padding-bottom: 0.65rem; overflow: hidden; width: 100%; }p + .mathjax-block { margin-top: -1.143rem; }.mathjax-block:not(:empty)::after { display: none; }[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: none; box-shadow: none; }:focus { outline: none; box-shadow: rgb(79, 172, 249) 0px 0px 2px 3px, rgb(120, 174, 218) 0px 0px 2px inset; }.task-list { list-style-type: none; }.task-list-item { position: relative; padding-left: 1em; }.task-list-item input { position: absolute; top: 0px; left: 0px; }.math { font-size: 1rem; }.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; }.md-toc-content { position: relative; margin-left: 0px; }.md-toc::after, .md-toc-content::after { display: none; }.md-toc-item { display: block; color: rgb(65, 131, 196); text-decoration: none; }.md-toc-inner:hover { text-decoration: underline; }.md-toc-inner { display: inline-block; cursor: pointer; }.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: bold; }.md-toc-h2 .md-toc-inner { margin-left: 2em; }.md-toc-h3 .md-toc-inner { margin-left: 4em; }.md-toc-h4 .md-toc-inner { margin-left: 6em; }.md-toc-h5 .md-toc-inner { margin-left: 8em; }.md-toc-h6 .md-toc-inner { margin-left: 10em; }.md-toc-h6 { margin-left: 12em; }@media screen and (max-width: 48em) { .md-toc-h3 .md-toc-inner { margin-left: 3.5em; } .md-toc-h4 .md-toc-inner { margin-left: 5em; } .md-toc-h5 .md-toc-inner { margin-left: 6.5em; } .md-toc-h5 .md-toc-inner { margin-left: 8em; } .md-toc-h6 { margin-left: 9.5em; }}a.md-toc-inner { color: inherit; font-size: inherit; font-style: inherit; font-weight: inherit; text-decoration: inherit; line-height: inherit; }.footnote-line a:not(.reversefootnote) { color: inherit; }.md-attr { display: none; }.md-fn-count::after { content: '.'; }.md-tag { opacity: 0.5; }h1 .md-tag, h2 .md-tag, h3 .md-tag, h4 .md-tag, h5 .md-tag, h6 .md-tag { font-weight: initial; opacity: 0.35; }html { font-size: 16px; }html, body { background-color: rgb(243, 242, 238); font-family: 'PT Serif'; color: rgb(31, 9, 9); line-height: 1.5em; }#write { max-width: 36em; }ol, ul { list-style: none; }blockquote, q { quotes: none; }blockquote::before, blockquote::after, q::before, q::after { content: none; }table { border-collapse: collapse; border-spacing: 0px; }h1, h2, h3, h4, h5, h6 { font-weight: bold; }h1 { font-size: 1.875em; line-height: 1.6em; margin-top: 2em; }h2, h3 { font-size: 1.3125em; line-height: 1.15; margin-top: 2.285714em; margin-bottom: 1.15em; }h3 { font-weight: normal; }h4 { font-size: 1.125em; margin-top: 2.67em; }h5, h6 { font-size: 1em; }h1 { border-bottom-width: 1px; border-bottom-style: solid; margin-bottom: 1.875em; padding-bottom: 0.8125em; }a { text-decoration: none; color: rgb(6, 85, 136); }a:hover, a:active { text-decoration: underline; }p, blockquote, pre.md-fences, .md-fences { margin-bottom: 1.5em; }h1, h2, h3, h4, h5, h6 { margin-bottom: 1.5em; }blockquote { font-style: italic; border-left-width: 5px; border-left-style: solid; margin-left: 2em; padding-left: 1em; }ul, ol { margin: 0px 0px 1.5em 1.5em; }ol li { list-style-type: decimal; list-style-position: outside; }ul li { list-style-type: disc; list-style-position: outside; }.md-meta, .md-before, .md-after { color: rgb(153, 153, 153); }table { margin-bottom: 1.5em; font-size: 1em; }thead th, tfoot th { padding: 0.25em 0.25em 0.25em 0.4em; text-transform: uppercase; }th { text-align: left; }td { vertical-align: top; padding: 0.25em 0.25em 0.25em 0.4em; }code, pre.md-fences { background-color: rgb(218, 218, 218); padding-left: 1ch; padding-right: 1ch; }pre.md-fences { margin-left: 2em; margin-bottom: 3em; }pre, code, tt { font-size: 0.875em; line-height: 1.714285em; }h1 { line-height: 1.3em; font-weight: normal; margin-bottom: 0.5em; }p + ul, p + ol { margin-top: -1em; }h3 + ul, h4 + ul, h5 + ul, h6 + ul, h3 + ol, h4 + ol, h5 + ol, h6 + ol { margin-top: 0.5em; }li > ul, li > ol { margin-top: inherit; }h2, h3 { margin-bottom: 0.75em; }hr { border-style: none none solid; border-bottom-width: 1px; }h1 { border-color: rgb(197, 197, 197); }blockquote { border-color: rgb(186, 186, 186); color: rgb(101, 101, 101); }thead.md-table-edit { background-color: transparent; }thead { background-color: rgb(218, 218, 218); }tr:nth-child(even) { background-color: rgb(232, 231, 231); background-position: initial initial; background-repeat: initial initial; }hr { border-color: rgb(197, 197, 197); }.task-list { padding-left: 1rem; }.task-list-item { padding-left: 1.5rem; list-style-type: none; }.task-list-item input::before { content: '√'; display: inline-block; width: 1.25rem; height: 1.5rem; vertical-align: middle; text-align: center; color: rgb(221, 221, 221); background-color: rgb(243, 242, 238); }.task-list-item input:checked::before, .task-list-item input[checked]::before { color: inherit; }#write pre.md-meta-block { min-height: 1.875rem; color: rgb(85, 85, 85); border: 0px; background-color: transparent; margin-left: 1em; margin-top: 1em; background-position: initial initial; background-repeat: initial initial; }.md-image > .md-meta { color: rgb(155, 81, 70); }.md-expand.md-image > .md-meta { background-color: rgba(255, 255, 255, 0.65098); }.md-image > .md-meta { font-family: Menlo, 'Ubuntu Mono', Consolas, 'Courier New', 'Microsoft Yahei', 'Hiragino Sans GB', 'WenQuanYi Micro Hei', sans-serif; }#write > h3.md-focus::before { left: -2.5rem; color: rgb(153, 153, 153); border-color: rgb(153, 153, 153); }#write > h4.md-focus::before { left: -2.5rem; top: 0.25rem; color: rgb(153, 153, 153); border-color: rgb(153, 153, 153); }#write > h5.md-focus::before { left: -2.5rem; color: rgb(153, 153, 153); border-color: rgb(153, 153, 153); }#write > h6.md-focus::before { left: -2.5rem; top: 0.3125rem; color: rgb(153, 153, 153); border-color: rgb(153, 153, 153); }.md-toc:focus .md-toc-content { margin-top: 19px; }.md-toc-content:empty::before { color: rgb(6, 85, 136); }.md-toc-item { color: rgb(6, 85, 136); }#write div.md-toc-tooltip { background-color: rgb(243, 242, 238); }#outline-dropmenu { background-color: rgb(243, 242, 238); -webkit-box-shadow: rgba(0, 0, 0, 0.372549) 0px 6px 12px; box-shadow: rgba(0, 0, 0, 0.372549) 0px 6px 12px; }.pin-outline #outline-dropmenu { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; box-shadow: none; border-right-width: 1px; border-right-style: dashed; background-position: inherit inherit; background-repeat: inherit inherit; }.pin-outline #outline-dropmenu:hover .outline-title-wrapper { border-left-width: 1px; border-left-style: dashed; }.outline-item:hover { background-color: rgb(218, 218, 218); border-left-width: 18px; border-left-style: solid; border-left-color: rgb(218, 218, 218); border-right-width: 18px; border-right-style: solid; border-right-color: rgb(218, 218, 218); }.outline-expander::before { content: ''; font-family: FontAwesome; font-size: 14px; top: 1px; }.outline-expander:hover::before, .outline-item-open > .outline-item > .outline-expander::before { content: ''; }

Merge Sort:

一.Motivation and Example

why study merge sort?

  • Good introduction to divide & conquer
  • Calibrate your preparation
  • Motivates guiding principles for algorithm analysis(worst-case and asymptotic analysis)
  • Analysis generalizes to “Master Method"

The Sorting Problem

Input: array of n numbers, unsorted. (5 4 1 8 7 2 6 3)

Output: same numbers, sorted in increasing order (1 … 8)

Example:

二.Pseudocode:

  • recursively sort 1st half of input array
  • recursively sort 2nd half of input array
  • merge two sorted sublists into one[ ignores base cases]

Pseudocode for Merge:

Merge Sort Running Time:

Key Question: running time of MergeSort on array of n numbers?

[Running time ~ #of mines of code executed ]

Running Time of Merge:

running time of merge on array of m numbers is <= 4m + 2 <= 6m(since m >= 1)

Running Time of Merge Sort

Claim: MergeSort requires <=

6nlog2n+6n

operations for sort n numbers

Recall:

log2n

of times you divide by 2 until you get a number that drops below one

So if you plug in 32 you got to divide five time by two get down to one.

三.Analysis

Claim:

For every Input array of n numbers, Merge Sort produces a sorted output array and uses at most 6nlog_2n + 6n operations.

Proof of calim (assuming n = power of 2):

Question1:

how many levels does this recursion tree have.===>

beacause: {level0, level1……. level(log2n)} === > (log_2n - 0) + 1 ===>

log2n+1

Question2

at each level j = 0,1 ,2 , log_2n, there are 2^j subproblems, each of size n / 2^j.

第二个的原因很简单, 这颗递归树的第j层的子问题有 2^j 个, 例如第0层是1个, 第1层是两个, 由此类推, 所以每个的大小都是n / 2^j.

Proof of claim

第j层总的合并程序中所含有的指令次数是 <= 2 ^j * 6(n / 2^j) = 6n.也就是所每一层的合并程序内部所做的指令次数都是固定的, 为6n次, 所以对于这课共有log2n + 1个层的数, 总的操作次数为6n( log2n + 1) = 6nlog_2n + 6n.

Divide and Conquer.(Merge Sort) by sixleaves的更多相关文章

  1. [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript

    Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...

  2. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  3. 归并排序(merge sort)

    M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...

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

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

  5. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  6. 【高级排序算法】1、归并排序法 - Merge Sort

    归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...

  7. 【算法】归并排序(Merge Sort)(五)

    归并排序(Merge Sort) 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...

  8. 复杂度分析 quick sort&merge sort

    空间复杂度看新开了什么数据结构就够了 公式=几个点*每个点执行了多少次 二叉树都是n次 二分法查找:lgn 全部查找:n n:找一个数,但是两边都要找.相当于遍历.类似于rotated sorted ...

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

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

随机推荐

  1. c语言结构体数组引用

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }dbdd[]={{,,}, {,,} };//初始化 void m ...

  2. IOS UIlabel设置文本距离边框距离

    自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...

  3. C# 酒鬼买酒喝,瓶盖和空瓶子可以换新的酒

        using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  4. 使用AWS的EC2服务器,进行跳墙

    选Ubuntu的服务器 一开始先设置Root,命令:su root 在安装之前,先Update一下源:apt-get update update完就可以安装: 1.安装pi,命令:apt-get in ...

  5. Eclipse+Java+OpenCV246环境搭建和代码测试

    1.首先下载OpenCV2.4.6,下载的时候,选择windows版的.然后安装 2.其实安装的过程就是解压的过程,并没有什么安装向导之类的,安装完成后,我们最关心的是这个目录:opencv\buil ...

  6. IPMITOOL常用操作指令

    一.开关机,重启 1. 查看开关机状态: ipmitool -H (BMC的管理IP地址) -I lanplus -U (BMC登录用户名) -P (BMC 登录用户名的密码) power statu ...

  7. Stm32高级定时器(三)

    Stm32高级定时器(三) 1 互补输出和死区插入 1.1 死区:某个处于相对无效状态的时间或空间 本来OCX信号与OCXREF时序同相同步,OCXN信号与OCXREF时序反相同步.但为了安全考虑,以 ...

  8. 可用与禁用 E:enabled { sRules }

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  9. 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”

    最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...

  10. IP地址基础和子网规划之其一

    IP地址的介绍:在TCP/IP环境中,各种各样的终端.工作站能同服务器.其他工作站无缝连接,是因为每一网络节点都使用了全网范围内能够唯一标识节点的IP地址.每个网络有一个全网唯一的网络号,在该网络中各 ...