上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 02 Divide and Conquer

上浒充通大学 SHANGHAI JLAO TONG UNIVERSITY Computer Algorithm Design and Analysis Lecture 2/Week 12:Divide and Conquer Yongxin ZHU,Weiguang SHENG 强 具是 3合 SHANG 1日G
Computer Algorithm Design and Analysis Lecture 2/ Week 12: Divide and Conquer Yongxin ZHU, Weiguang SHENG

上游充通大粤 Recursive algorithms SHANGHAI JIAO TONG UNIVERSITY General idea: Divide a large problem into smaller ones ·By a constant ratio By a constant or some variable Solve each smaller one recursively or explicitly Combine the solutions of smaller ones to form a solution for the original problem Divide and Conquer 3/12/2022 2
3/12/2022 2 Recursive algorithms General idea: • Divide a large problem into smaller ones • By a constant ratio • By a constant or some variable • Solve each smaller one recursively or explicitly • Combine the solutions of smaller ones to form a solution for the original problem Divide and Conquer

上浒充通大学 Divide-and-Conquer SHANGHAI JIAO TONG UNIVERSITY Divide-and-conquer. Break up problem into several parts. e Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage. Break up problem of size n into two equal parts of size vn. Solve two parts recursively. Combine two solutions into overall solution in linear time. ©Consequence. ·Brute force:n2. Divide et impera. Veni,vidi,vici. Divide-and-conquer:n log n. Julius Caesar
3 Divide-and-Conquer Divide-and-conquer. • Break up problem into several parts. • Solve each part recursively. • Combine solutions to sub-problems into overall solution. Most common usage. • Break up problem of size n into two equal parts of size ½n. • Solve two parts recursively. • Combine two solutions into overall solution in linear time. Consequence. • Brute force: n2. • Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar

上游充通大学 SHANGHAI JLAO TONG UNIVERSITY Divide and Conquer Example1:Merge Sort 强 LAMAANAMA A学 SHANG 1日G
Divide and Conquer Example1: Merge Sort

上游充通大学 Sorting SHANGHAI JLAO TONG UNIVERSITY Sorting.Given n elements,rearrange in ascending order. Obvious sorting applications. Non-obvious sorting applications. List files in a directory. Data compression. Organize an MP3 library. Computer graphics. List names in a phone book. Interval scheduling. Display Google PageRank Computational biology. results. Minimum spanning tree. Supply chain management Problems become easier once Simulate a system of particles. sorted. Book recommendations on Find the median. Amazon. Find the closest pair. Load balancing on a parallel Binary search in a computer. database. Identify statistical outliers. Find duplicates in a mailing list
5 Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results. Problems become easier once sorted. Find the median. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list. Non-obvious sorting applications. Data compression. Computer graphics. Interval scheduling. Computational biology. Minimum spanning tree. Supply chain management. Simulate a system of particles. Book recommendations on Amazon. Load balancing on a parallel computer. . . . Sorting Sorting. Given n elements, rearrange in ascending order

上游充通大学 Mergesort SHANGHAI JIAO TONG UNIVERSITY ©Mergesort.. Divide array into two halves. Recursively sort each half. Jon von Neumann(1945) Merge two halves to make sorted whole. A G R 工TH MS A G R divide 0(1) A G L R HI M S T sort 2T(n/2) A merge O(n)
6 Mergesort Mergesort. • Divide array into two halves. • Recursively sort each half. • Merge two halves to make sorted whole. merge sort divide A L G O R I T H M S A L G O R I T H M S A G L O R H I M S T A G H I L M O R S T Jon von Neumann (1945) O(n) 2T(n/2) O(1)

上浒充通大学 Merging SHANGHAI JIAO TONG UNIVERSITY Merging.Combine two pre-sorted lists into a sorted whole. How to merge efficiently? Linear number of comparisons. ·Use temporary array. R H工M S T A G H I Challenge for the bored.In-place merge.[Kronrud,1969] using only a constant amount of extra storage
7 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? • Linear number of comparisons. • Use temporary array. Challenge for the bored. In-place merge. [Kronrud, 1969] A G L O R H I M S T A G H I using only a constant amount of extra storage

上游充通大学 So Easy? SHANGHAI JIAO TONG UNIVERSITY ©合并排序 对于一个要排序的数组,把它分为两个长度大致相同的部分,并 对每个长度大于1的子数组进行递归排序,然后将这两个排好序的 数组合并就可以得到一个有序数组。合并的方法是:设置两个指 针和j,分别指向两个有序子数组的第一个元素,然后比较它们指 向的值,取其中较小者放入到结果数组中,并将相应指针后移一 个位置。重复此过程直到某个子数组的元素全部加入到结果数组 中为止。如果此时另一个数组中仍有元素,只需将剩余元素复制 到结果数组的末尾即可。吴哲辉等 《算法设计方法》 取其中较小者放入到结果数组中,并将相应指针后移一个位置? ©找到一边的大的值后,应该移动另外一边的指针,一直移到边 界或者另外一边的值更大。 8
8 So Easy? 合并排序 对于一个要排序的数组,把它分为两个长度大致相同的部分,并 对每个长度大于1的子数组进行递归排序,然后将这两个排好序的 数组合并就可以得到一个有序数组。合并的方法是:设置两个指 针i和j,分别指向两个有序子数组的第一个元素,然后比较它们指 向的值,取其中较小者放入到结果数组中,并将相应指针后移一 个位置。重复此过程直到某个子数组的元素全部加入到结果数组 中为止。如果此时另一个数组中仍有元素,只需将剩余元素复制 到结果数组的末尾即可。 取其中较小者放入到结果数组中,并将相应指针后移一个位置? 找到一边的大的值后,应该移动另外一边的指针,一直移到边 界或者另外一边的值更大。 吴哲辉等 《算法设计方法》

上浒充通大学 Merge sort SHANGHAI JIAO TONG UNIVERSITY MERGE-SORT A[I.·n] 1.Ifn=1.done 2.Recursively sort 4[1..Tn/21] andA[「n/2+1..n]. 3.Merge"the 2 sorted lists. Key subroutine:MERGE 3/12/2022 9
3/12/2022 9 Merge sort MERGE-SORT A[1 . . n] 1. If n = 1, done. 2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . 3. “Merge” the 2 sorted lists. Key subroutine: MERGE

上游充通大粤 Merging two sorted arrays SHANGHAI JIAO TONG UNIVERSITY Subarray 1Subarray 2 20 21 7 2 1 3/12/2022 10
3/12/2022 10 20 13 7 2 Merging two sorted arrays 12 11 9 1 Subarray 1Subarray 2
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 01 Greedy and Dynamic Programming.pptx
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 01 算法设计与分析基础.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 09 C程序组织.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 08 指针.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 07 函数.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 06 C语言数组.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 05 C语言语句.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 03 C语言数据类型.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 02 C语言简介.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 15 输入输出.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 14 内存检测、剖面分析.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 13 高级指针.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 12 结构、联合与枚举.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 11 字符串.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 10 C程序调试.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 01 课程简介及编程基础(绳伟光).pdf
- 机械工业出版社:计算机科学丛书《计算机组成与设计:硬件、软件接口》电子教材(中文第4版).pdf
- 《计算机系统结构》课程教学资源(电子书籍)《Computer Systems》A Programmer's Perspective(Randal E. Bryant、David R. O'Hallaron,THIRD EDITION).pdf
- 《计算机系统结构》课程教学资源(电子书籍)《Computer Organization and Design》THE HARDWARE / SOFTWARE INTERFACE(DAVID A. PATTERSON JOHN L. HENNESSY,Fourth Edtion,彩色版).pdf
- 《中文信息学报》:中文组织机构名称与简称的识别.pdf
- 上海交通大学:《C程序与算法设计》课程教学资源(课件讲稿)Lecture 04 C语言运算符与表达式.pdf
- 《C程序与算法设计》课程教学资源(学习资料)快乐的Linux命令行.pdf
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)01 ROS系统安装.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)02 ROS基本元素实验(一).doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)03 ROS基本元素实验(二).doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)04 调试和可视化.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)05 外部设备的使用.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)06 机器视觉.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)07 机器人建模与仿真.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)08 机器人导航包.doc
- 上海交通大学:《ROS机器人操作系统基础与实战》课程教学资源(实验指导书)09 机械臂规划Moveit.doc
- 《并行与分布式程序设计》课程教学参考书:CUDA C PROGRAMMING(CUDA编程指南4.0中文版).pdf
- 《并行与分布式程序设计》课程教学参考书:NVIDIA《CUDA C PROGRAMMING GUIDE》(Design Guide,CHANGES FROM VERSION 9.0).pdf
- 《并行与分布式程序设计》课程教学参考书:NVIDIA《CUDA C Programming》(Professional).pdf
- 《并行与分布式程序设计》课程教学参考书:CUDA《Programming Massively Parallel Processors》A Hands-on Approach(美,David B. Kirk and Wen-mei W. Hwu,英文版).pdf
- 《并行与分布式程序设计》课程教学参考书:CUDA《Programming Massively Parellel Processors》大规模并行处理器编程实战(美)David B.Kirk&Wen-mei W.Hwu(中文版).pdf
- 《并行与分布式程序设计》课程教学参考书:分布式与云计算(美)Tom White《Hadoop权威指南》(中文第3版).pdf
- 《并行与分布式程序设计》课程教学参考书:分布式与云计算《Spark大数据处理技术、应用与性能优化》(PDF扫描版).pdf
- 《并行与分布式程序设计》课程教学参考书:并行与并发编程《An Introduction to Parallel Programming》.pdf
- 《并行与分布式程序设计》课程教学参考书:并行与并发编程《C++ Concurrency in Action - Practical Multithreading》(Manning, 2012).pdf