一、

方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22)。例如,
下面给出了 BinarySearch 的 rank() 方法的另一种实现。我们会经常使用递归,因为递归代码比
相应的非递归代码更加简洁优雅、易懂。下面这种实现中的注释就言简意赅地说明了代码的作用。
我们可以用数学归纳法证明这段注释所解释的算法的正确性。我们会在 3.1 节中展开这个话题并为
二分查找提供一个这样的证明。
编写递归代码时最重要的有以下三点。
‰ 递归总有一个最简单的情况——方法的第一条语句总是一个包含 return 的条件语句。
‰ 递归调用总是去尝试解决一个规模更小的子问题,这样递归才能收敛到最简单的情况。在下
面的代码中,第四个参数和第三个参数的差值一直在缩小。

递归调用的父问题和尝试解决的子问题之间不应该有交集。在下面的代码中,两个子问题各
自操作的数组部分是不同的。

 public static int rank(int key, int[] a, int lo, int hi) {
//如果key存在于a[]中,它的索引不会小于lo且不会大于hi
if (lo > hi) return -1;
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) return rank(key, a, lo, mid - 1);
else if (key > a[mid]) return rank(key, a, mid + 1, hi);
else return mid;
}

非递归算法

     /**
* Returns the index of the specified key in the specified array.
*
* @param a the array of integers, must be sorted in ascending order
* @param key the search key
* @return index of key in array <tt>a</tt> if present; <tt>-1</tt> otherwise
*/
public static int indexOf(int[] a, int key) {
// 数组必须是有序的
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}

违背其中任意一条都可能得到错误的结果或是低效的代码(见练习 1.1.19 和练习 1.1.27),而

坚持这些原则能写出清晰、正确且容易评估性能的程序。使用递归的另一个原因是我们可以使用数
学模型的来估计程序的性能。我们会在 3.2 节的二分查找以及其他几个地方分析这个问题。

算法Sedgewick第四版-第1章基础-001递归的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  7. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  8. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

随机推荐

  1. (转)如何在高并发分布式系统中生成全局唯一Id

    又一个多月没冒泡了,其实最近学了些东西,但是没有安排时间整理成博文,后续再奉上.最近还写了一个发邮件的组件以及性能测试请看 <NET开发邮件发送功能的全面教程(含邮件组件源码)> ,还弄了 ...

  2. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  3. 创建型模式——Builder

    1.意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.结构 3.参与者 Builder为创建一个Product对象的各个部件指定抽象接口 ConcreteBuild ...

  4. httpd配置Gzip压缩

    以下设置在 /etc/httpd/conf/httpd.conf 文件末尾加入即可.(不同方式安装的httpd可能主配置文件位置不同,请自行查找) 一.mod_deflate模块:文件压缩 官方文档: ...

  5. Android 源码VecotorDrawable

    1 R.styleable.VectorDrawable_viewportWidth 该资源的名字并非VectorDrawable_viewportWidth 而是 attrs.xml 下的声明 &l ...

  6. 51nod1265四点共面

    1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共线也算共面).如 ...

  7. shopnc 商城源码阅读笔记--开篇概述

    关于shopnc 以下是摘抄自百度百科的关于shopnc的介绍: ShopNC商城系统,是天津市网城天创科技有限责任公司开发的一套多店模式的商城系统. 本系统具有商城系统非常完整和专业的功能与流程,系 ...

  8. laravel扩展图片处理Intervention Image

    github地址:https://github.com/Intervention/image

  9. Delphi-仿vb里的IIF函数

    //Delphi 函数-IIF // 实例-ChkValue := IIF(TCheckBox(tsPzJz.Controls[i]).Checked, '); function IIF(lExp: ...

  10. ubuntu下安装ffmpeg和X264

    第一步:安装必要的库 $:-dev libtheora-dev libx11-dev zlib1g-dev 第二步:安装SDL(否则可能编译不出ffplay) $:-dev $:-dev libsdl ...