recursive and iterative algorithms
Download
Skip this Video
Download Presentation
Recursive and Iterative Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 8

recursive and iterative algorithms - PowerPoint PPT Presentation


  • 269 Views
  • Uploaded on

Recursive and Iterative Algorithms. 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. Now this Algorithm?. Algorithm now-what (n)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'recursive and iterative algorithms' - MikeCarlo


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what does the following algorithm do
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.

Debasis Mitra, FIT

now this algorithm
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.

Debasis Mitra, FIT

actually the recursion is working like
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.

Debasis Mitra, FIT

recursion tree for recursive fibonacci number calculation sample
Recursion tree for recursive Fibonacci number calculation: sample

n-w(4)

n-w(2)

n-w(3)

n-w(0)

n-w(1)

n-w(2)

n-w(1)

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

Debasis Mitra, FIT

binary search recursive algorithm
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) if key >A[mid]

(5) return bin-search(A, mid+1, l);

else

(6) return bin-search(A, f, mid);

end if;

end if;

End algorithm.

Driver: bin-search (A, 1, n).

Debasis Mitra, FIT

binary search iterative algorithm stack based
Binary Search iterative algorithm: stack-based

Algorithm bin-search(sorted array A, key)

(1) create stack; int f := 1; int l := size_of(A);

(3)push(stack, (f,l));

(4) while stack not empty do

(5) (f, l) := pop(stack);

(6) if f = = l then

(7) if key = = A[f] then return f

(8) else return failure;

else

(9) int mid := (f+l)/2;

(10) if key > A[mid] then

(11) push (stack, (mid+1, l));

else

(12) push (stack, (f, mid));

end if;

end while;

End algorithm.

Debasis Mitra, FIT

binary search iterative algorithm non stack based
Binary Search iterative algorithm: non-stack based

Algorithm bin-search(sorted array A, key)

(1) int f := 1; int l := size_of(A);

(2) While fl do

(3) int mid := (f + l)/2;

(4) if key > A[mid] then

(11) f := mid +1;

else

(12) l := mid;

end while;

(13)If key = = A[f] then return f

(14) else return failure;

End algorithm.

Debasis Mitra, FIT

ad