a02-chap2-framework 算法导论 教学课件.pptVIP

  • 4
  • 0
  • 约1.63万字
  • 约 35页
  • 2018-01-25 发布于浙江
  • 举报
a02-chap2-framework 算法导论 教学课件

* * * * * * * * * How to set up the recurrence for T(n), the worst-case running time of merge sort on n numbers? If one element n=1, takes constant time. When n1, Divide: The step just computes the middle of the subarray, one step. Thus, D(n)=Θ(1). Conquer: Recursively solve two subprolems, each of size n/2, which contributes 2T(n/2) to the running time. Combine: MERGE procedure shows C(n)=Θ(n) . 2.3.2 Analyzing divide-and-conquer algorithms MERGE-SORT(A, p, r) 1 if p r 2 Then q ← 3 MERGE-SORT(A, p, q) 4 MERGE-SORT(A, q+1, r) 5 MERGE(A, p, q, r) * 2.3.2 Analyzing divide-and-conquer algorithms Rewrite the recurrence equation solve the recurrence equation. * 2.3.2 Analyzing divide-and-conquer algorithms Add up the costs of all the levels. There are lgn+1 levels, each costing cn, for a total cost obviously, it outperforms insertion sort, whose running time is Θ(n2) , in the worst case, for large enough inputs. * Exercises and problems Exercises 分析Fibonacci序列 F(n) = F(n-1) + F(n-2) 如下算法的时间复杂度。 re_type f(type n) { if(n=2) return 1; else return f(n-1)+f(n-2); } f1 = 1; f2 = 1; for(i=3; i=n; i++){ f = f1 + f2; f1 = f2; f2 = f; } * Exercises and problems Exercises 2.3-1, 2.3-3, 2.3-5(编程), 2.3-6, 2.3-7(分析并编程实现) 补充:自己编程实现几种常用的排序算法(插入、冒泡、归并、快排、等等),并比较分析其运算时间。输入为随机产生的n个数,让n尽可能地大。 Problems 2-1, 2-3 (a, b) * * * * * * * * * * * * * * * * * * * * * * * * * * * * BeihangSoft.cn * * 2 Getting Started Goals: Start using framework for describing and analyzing algorithms (描述和分析算法的框架) Examine two algorithms for sorting: insertion sort and merge sort See how to describe algorithms in pseudocode, C/C++ and argue the correctness of that (如何描述算法) Begin using asymptotic notation to express running-time analysis (使用一致性符号来分析算法的运算时间) Learn the technique of “divide and conquer” in the context of merge sort (通过merge sort来学习分而治之方法) * 2 Getting Started 2.1 Insertion so

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档