Algorithm in C by Sedgewick Reading Notes

Source: Internet
Author: User

Update: July 4, 2014

Chap 5:

At the beginning, he mentioned that: recursion is a divide-and-conquer method. although extends algorithms can be solved in simple recursion but it is often an equally algorithm lies in the details of a nonrecursive implementation? (Not understood)

Divide-and-conquer programs normally do not reduce to trivial loops since they have 2 recursive cballs and do had divided without overlap so no excessive recomputing.

/** method 1 of ruler: @ p54*/rule(int l, int r, int h){   ...   mark(m,h);   rule(l, m, h-1);    // NOTICE: not to m-1;   rule(m, r, h-1);   // NOTICE: not from m+1;}

As shown in Fig 5.4. The tree plot of calling recursive method. shows the difference of changing the order of calling 2 recursions.

In compiler, it actually removes recursion. one well-known tech is end-recursion removal. firstly use goto instead of looping. then remove the 2nd recursion (this is easy since there is no code after it ). but the other recursion needs more work to remove, which is by using the normal way for any procedure call: "push the values of local variables and the address of the next instruction on a stack, set the values of parameters to the procedure and goto the beginning of the procedure. "Then, when the procedure has completes, it must" pop the return address and values of local variables from Stack (note the order because the stack is LIFO), reset the variables, and goto the return address."

This is the same as the way Sedgewick's code:

traverse(struct node *t){l:   if(t==z) goto s;    visit(t);    push(t); t=t->l; goto l;  // when call procedure, push var, return addr into stack. Then                // change the var and goto the begining of the procedure. Also, when complete               // it should go to s, which pop var and goto return addr.r:  t = t->r; goto l;         s: if(stackEmpty()) goto x;  // when complete, it pop return addr, var out and goto the return addr.\    t = pop(); goto r;            // since the return addr is the ending recursive which is a constant, so x: ;                 // don‘t need to push or pop the addr. Also,}

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.