递归(recursion) 详解 及 代码
本文地址:http://blog.csdn.net/caroline_wendy/article/details/17099693
递归(recursive)是一种程序设计方式, 可以使程序精巧(compact)和优雅(elegant);
递归包含三个规则(rules):
1. 递归需要一个基本示例(base case), 且第一个语句(statement)必须是一个返回语句(return);
2. 递归的子问题是基本示例的较小(smaller)的问题, 并且收敛(converge)于基本示例(base case);
3. 递归的子问题必须是互斥(disjoint), 不能重叠(ovelap);
非递归模式:http://blog.csdn.net/caroline_wendy/article/details/17068019
使用递归的二分查找, 代码如下:
/*
* Algorithms.java
*
* Created on: 2013.12.03
* Author: Wendy
*/
/*eclipse std kepler, jdk 1.7*/
import java.util.Arrays;
public class Algorithms
{
public static int rank(int key, int[] a)
{ return rank(key, a, 0, a.length-1); }
public static int rank(int key, int[] a, int lo, int 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;
}
public static void main(String[] args)
{
int[] a = {1, 3, 4, 5, 2, 6, 7, 8, 9, 0};
Arrays.sort(a);
int[] b = {4, 11, 5, 12};
for(int i=0; i<b.length; ++i) {
int p = rank(b[i], a);
if(-1 == p) {
System.out.println("failed to search " + b[i] + " : ( ");
} else {
//数组是有序的
System.out.println("the " + b[i] + " position is " + p + " : ) ");
}
}
}
}
输出:
the 4 position is 4 : )
failed to search 11 : (
the 5 position is 5 : )
failed to search 12 : (
分享到:
相关推荐
Algorithm-Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.zip,使用python的算法和数据结构解决问题的代码,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Machine_Learning_Algorithms-master,Machine_Learning_Algorithms-master 配套数据集及源代码 Machine_Learning_Algorithms-master,Machine_Learning_Algorithms-master 配套数据集及源代码
problem-solving-with-algorithms-and-data-structure-using-python 中文版
python-algorithms-mastering-basic-algorithms-in-the-python-language.9781430232377.53502.pdf
python-algorithms-mastering-basic-algorithms-in-the-python-language(英文正版)1
Neo4j,用户手册,涵盖所有集成的图算法及应用场景,非常适合图算法的学习和应用
data-algorithms-book, 数据算法书的MapReduce Spark Java和 Scala 数据算法。作者:Mahmoud Parsian ( mahmoud.parsian@yahoo.com )标题:数据算法:使用Hadoop和 Spark 扩展的食谱。这个GitHub存储库将托管所有的...
jung-algorithms-201-modified-sources:进行了一些小修改的jung-algorithms-2.0.1项目的源代码
A-Comparative-Study-of-Reco-mmendation-Algorithms-in-E-Commerce-Applications
This e-book is devoted to global optimization algorithms, which are methods to find optimal solutions for given problems.
这些笔记教会学生抽象地思考算法以及用于开发它们的关键算法技术。
This paper presents the top 10 data mining algorithms identified by the IEEE International Conference on Data Mining (ICDM) in December 2006: C4.5, k-Means, SVM, Apriori, EM, PageRank, AdaBoost, kNN, ...
Beginning Algorithms----学习算法的好书,介绍各种算法!!!
遗传算法Python程序 Hands-On-Genetic-Algorithms-with-Python-master.zip
Study-guide-for-Algorithms-in-Bioinformatics-A-Practical-Introduction
Algorithms - Robert Sedgewick, Kevin Wayne Algorithms - Robert Sedgewick, Kevin Wayne 视频 算法 普林斯顿
Image-Fusion-Algorithms-and-Applications.pdf
Algorithm-Algorithms-and-Data-Structures-in-Java.zip,Java中的算法和数据结构,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
ALGORITHMS - ROBERT SEDGEWICK
Genetic-Algorithms-with-Python.pdf