Some Classical Recursive Functions

*/-->

Some Classical Recursive Functions

Table of Contents

  • 1. Find the maximum element of an array recursively.

In this post, I will list the commonly met recursive functions that can light
the road for further investigation into the magic world of computer science.
I am not an excellent programmer myself, but through delight practice, the
little but essential part of the RECURSIVE PROBLEMS can be understood. So I will
present some examples to illustrate what I mean.

1 Find the maximum element of an array recursively.

#define max(a,b)     (a) > (b) ? (a) : (b)

int f_max(int *a,int start,int end)
{
    if (start == end) {
        return a[start];
    }
    return  max(a[start],f_max(a,start + 1,end));
}

void test_max(void)
{
    int a[] = {
        12,1,22,56,4
    };
    printf("max=%d\n",f_max(a,0,4));
}

Author: wujing

Created: 2014-07-13 日 10:45

Emacs 24.3.1 (Org mode 8.2.6)

Validate

Some Classical Recursive Functions

时间: 2024-10-21 20:14:08

Some Classical Recursive Functions的相关文章

Recursive functions and algorithms

http://en.wikipedia.org/wiki/Recursion_(computer_science)#Recursive_functions_and_algorithms A common computer programming tactic is to divide a problem into sub-problems of the same type as the original, solve those sub-problems, and combine the res

scala tail recursive优化,复用函数栈

在scala中如果一个函数在最后一步调用自己(必须完全调用自己,不能加其他额外运算子),那么在scala中会复用函数栈,这样递归调用就转化成了线性的调用,效率大大的提高.If a function calls itself as its last action, the function's stack frame can be reused. This is called tail recursion.=> Tail recursive functions are iterative proc

Function Expression

One of the key characteristics of function declarations is function declaration hoisting, whereby function declarations are read before the code excutes. That means a function declaration may appear after code that calls it and still work: 1 sayHi();

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20

jquery 2.1.0 源码

/*! * jQuery JavaScript Library v2.1.0 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2

Python Foundation - part one

Useful Tips 在windows下安装python包出现错误,缺少setuptool, 此时先下载安装ez_setup.py, 然后就可安装所需包 Development Now, I changed to Linux, forget windows, it really sucks to do development not .NET on Windows, So I nuked up my windows and installed solely Ubuntu14.04, and p

Sizzle源码分析:三 筛选和编译

好了有了之前的词法分析过程,现在我们来到select函数来,这个函数的整体流程,前面也大概说过: 1. 先做词法分析获得token列表 2. 如果有种子集合直接到编译过程 3. 如果没有种子集合并且是单组选择符(没有逗号) (1)尝试缩小上下文:如果第一个token是ID选择符,则会执行Expr.find["ID"]的方法来找到这个上下文,以后所有的查询都是在这个上下文进行,然后把第一个ID选择符剔除. (2)尝试寻找种子集合:从右开始往左分析token,如果遇到关系选择符(>

Python算法:推导、递归和规约

Python算法:推导.递归和规约 注:本节中我给定下面三个重要词汇的中文翻译分别是:Induction(推导).Recursion(递归)和Reduction(规约) 本节主要介绍算法设计的三个核心知识:Induction(推导).Recursion(递归)和Reduction(规约),这是原书的重点和难点部分 正如标题所示,本节主要介绍下面三部分内容: • Reduction means transforming one problem to another. We normally red

Universal Programs

The Universal Function for n-ary computable functions is the (n+1)-ary function: Theorem. For each n, the universal function ΨU is computable. ============================================================================================ To avoid using