算法导论第九章习题答案(第三版) Introduction to Algorithm

Exercise

9.1-1

对所有的元素,两个一组进行比较,共需n-1次比较,可以构成一棵二叉树,最小的元素在树的根结点上,接下来,画出二叉树,可以很容易的看出共需lgn-1次比较,所以共需n+lgn-2次比较才可以找出第二小的元素。

9.1-2

略。

9.2-1

在randomized-select中,对于长度为0的数组,此时p=r,直接返回A[p],所以不会进行递归调用。

9.2-2

略。

9.2-3

RANDOMIZED-SELECT(A,p,r,i){
    while(true){
        if(p==r)
            return A[p];
        q=RANDOMIZED-PARTITION(A,p,r);
        k=q-p+1;
        if(i==k)
            return A[q];
        else if(i<k)
            q--;
        else{
            q++;
            i-=k;
        }
    }
}

9.2-4

每次都以最大的元素进行划分即可。

9.3-1

数学计算,根据书中例题仿照分析即可。

9.3-3

随机化

9.3-5

类似主元划分,只要把黑箱子输出的值作为主元划分去选择即可。

9.3-6

多重二分即可。

9.3-7

算出中位数,之后算出每一个数与中位数的差即可。

9.3-8

分别取两个数组的中位数进行比较,如果两个中位数相等,那么即为所求,否则,取中位数较小的一个的右边,取较大的一个的右边,直到就剩4个元素为止,这时候只要求这4个元素的中位数即可。

 

 

转载于:https://www.cnblogs.com/hlj-ljz/p/3515860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
May 2001 Part I: Foundations Chapter List Chapter 1: The Role of Algorithms in Computing Chapter 2: Getting Started Chapter 3: Growth of Functions Chapter 4: Recurrences Chapter 5: Probabilistic Analysis and Randomized Algorithms Introduction This part will get you started in thinking about designing and analyzing algorithms. It is intended to be a gentle introduction to how we specify algorithms, some of the design strategies we will use throughout this book, and many of the fundamental ideas used in algorithm analysis. Later parts of this book will build upon this base. Chapter 1 is an overview of algorithms and their place in modern computing systems. This chapter defines what an algorithm is and lists some examples. It also makes a case that algorithms are a technology, just as are fast hardware, graphical user interfaces, objectoriented systems, and networks. In Chapter 2, we see our first algorithms, which solve the problem of sorting a sequence of n numbers. They are written in a pseudocode which, although not directly translatable to any conventional programming language, conveys the structure of the algorithm clearly enough that a competent programmer can implement it in the language of his choice. The sorting algorithms we examine are insertion sort, which uses an incremental approach, and merge sort, which uses a recursive technique known as "divide and conquer." Although the time each requires increases with the value of n, the rate of increase differs between the two algorithms. We determine these running times in Chapter 2, and we develop a useful notation to express them. Chapter 3 precisely defines this notation, which we call asymptotic notation. It starts by defining several asymptotic notations, which we use for bounding algorithm running times from above and/or below. The rest of Chapter 3 is primarily a presentation of mathematical notation. Its purpose is more to ensure that your use of notation matches that in this book than to teach you new mathematical concepts. Chapter 4 delves further into the divide-and-conquer method introduced in Chapter 2. In particular, Chapter 4 contains methods for solving recurrences, which are useful for describing the running times of recursive algorithms. One powerful technique is the "master method," which can be used to solve recurrences that arise from divide-and-conquer algorithms. Much of Chapter 4 is devoted to proving the correctness of the master method, though this proof may be skipped without harm.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值