{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

RecAndItAlg - Recursive and Iterative Algorithms Debasis...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
02/10/12 Debasis Mitra, FIT 1 Recursive and Iterative Algorithms
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
02/10/12 Debasis Mitra, FIT 2 What does the following algorithm do? Algorithm what(n) (1) x:=1; y:=1; z:=0; (2) for int I:=1 to n-2 do (3) z := x +y; (4) y := x; (5) x := z; end for; (6) return z; End algorithm.
Image of page 2
02/10/12 Debasis Mitra, FIT 3 Now this Algorithm? Algorithm now-what (n) (1) if n = = 1 or 0 then return 1 (2) else (3) return now-what (n-1) + now-what (n-2) (4) end if; End algorithm.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
02/10/12 Debasis Mitra, FIT 4 Actually the recursion is working like: Algorithm now-what ( n ) (1)create stack ; (2)if n>1 then push ( stack , n ); (3)int temp := 0; (4)while stack not empty do (5) int x := pop ( stack ); (6) if x > 1 then (7) push ( stack , x -1); (8) push ( stack , x -2); else (9) temp := temp + 1; end if; end while; (10) return temp; End algorithm.
Image of page 4
02/10/12 Debasis Mitra, FIT 5 Recursion tree for recursive Fibonacci number calculation: sample n-w(4) n-w(1) n-w(2) n-w(1) n-w(0) n-w(3) n-w(2) n-w(1) n-w(0) n-w (4) = n-w (1) + n-w (0) + n-w (1) + n-w (1) + n-w (0) = 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
02/10/12 Debasis Mitra, FIT 6 Binary search recursive algorithm Algorithm bin-search (sorted array A , index f , index l , key ) (1)If f = = l (2) If key = = A[f] then return f else return failure ; else (3) int mid = ( f + l )/2; (4)
Image of page 6
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern