Simple analysis of recursion of "Java implementation of algorithm data structure" and computation of time complexity

Source: Internet
Author: User
Tags benchmark

1. Understanding of the recursive function of understanding, I think it is more important, because many great gods can use recursive functions of the lifelike, not only their programming skills, but also mainly to understand the algorithm. The straightforward understanding is that if the logic of an event can be expressed in the form of f (x) =nf (x-1) +o (x), then it can be achieved by recursive thinking.
When writing recursive logic, you should know the following rules: 1. To have a benchmark, for example, f (x) =f (x-1) +1, if the value of f (0) is not added to the benchmark, then the function will execute indefinitely without meaning 2. Continuous propulsion is f (x) =f (x-1) or F (x) =f (x/n) Yes, of course. Each recursive function has a more magical step, which is the backtracking step, for example:    Fact ( 3)-----Fact (2)-----Fact (1)------fact (2)-----Fact (3)------------------------------>------------------------------>
Recursive backtracking

2. Example implementations
calculation and f (x) = 0, first list the formula F (x) =f (x-1) +x/(4**x) (Two * * represents the second party, Python is used to), get the following code
public class Recursion {public     static void Main (String args[]) {         System.out.print (f (2));   }   public static double F (int x) {   if (x==0) {   return 0;   }   Else{   return F (x-1) +x/math.pow (4,x);}}   

The result is: f (2) = 0.375, verify correct

3. Calculation of time complexity
The above is an example of the expansion of F (x) =f (x-1) +x/(4**x),

Subtract f (x) by 4, so that the x side of 4 is equal to K, then the primitive time complexity, and log is the base of 4.





Reference: http://blog.csdn.net/budapest/article/details/6367973

/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/




Simple analysis of recursion of "Java implementation of algorithm data structure" and computation of time complexity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.