一、

方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 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. 【转载】绝对干货!Linux小白最佳实践:《超容易的Linux系统管理入门书》(连载九)如何通过源代码安装软件

    除了使用Linux的包管理机制进行软件的安装.更新和卸载,从源代码进行软件的安装也是非常常见的,开源软件提供了源代码包,开发者可以方便的通过源代码进行安装.从源码安装软件一般经过软件配置.编译软件.执 ...

  2. DP入门数塔问题

    在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?        已经告诉你了,这 ...

  3. web响应式之bootstrap的基础用法。

    1/首先必须在head里面引用视窗viewport,以保证之后可以响应式分布 <!--meta:vp 响应式布局--> <meta name="viewport" ...

  4. checkbox批量选中,获取选中的项的值

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  5. 解读Python内存管理机制

    转自:http://developer.51cto.com/art/201007/213585.htm 内存管理,对于Python这样的动态语言,是至关重要的一部分,它在很大程度上甚至决定了Pytho ...

  6. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  7. python 控制台输出中文乱码问题

    乱码原因: 源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了! 解决方法: 1,print mystr.decode('utf ...

  8. zhuan: ubuntu 安装 apache2

    安装 用  sudo apt-get install apache2 sudo /etc/init.d/apache2 restart 如果发现错误: 以下from:http://cache.baid ...

  9. 使用saltstack批量部署服务器运行环境事例——批量部署nagios客户端

    之前关于搭建web服务器集群实验的这篇文章http://www.cnblogs.com/cjyfff/p/3553579.html中,关于如何用saltstack批量部署服务器这一点当时没有记录到文章 ...

  10. C# 读取oracle 中文乱码的解决方案

    用OracleDataAccess.dll访问oracle数据库,遇到中文乱码的情况. 解决方案如下: 1查看字符集编码, 在数据库服务器端 启动 sqlplus SQL->select use ...