MSIT 127 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

MSIT 127

Description:

Fibonacci Numbers. Binomial Coefficients. Search. Sorting. FACTORIAL. A ... Fib(n) = Fib(n-1) Fib(n-2) for n 2. The Fibonacci series: 1,1,2,3,5,8,13,21. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 17
Provided by: asp77
Category:
Tags: msit | fibonacci

less

Transcript and Presenter's Notes

Title: MSIT 127


1
MSIT 127
  • Data Structures and Algorithms

2
RECURSION
  • What is Recursion?
  • it is a function or procedure that calls itself.
  • it is a powerful tool for repetitive task
  • Advantage(s)
  • it makes algorithm compact
  • Simple and easy to debug
  • Disadvantage(s)
  • requires more memory space
  • Slow compared to iteration

3
  • Characteristics of recursion
  • Repetitively Calls itself, thus making a Clone of
    itself
  • It has a terminating condition (Base Value)
  • Every Call, it moves closer to the terminating
    condition

4
  • Some Applications
  • Factorial
  • Fibonacci Numbers
  • Binomial Coefficients
  • Search
  • Sorting

5
FACTORIAL
  • A factorial 5 is evaluated as
  • 5! 5 4 3 2 1
  • 5! 120
  • Rewriting the relation,
  • 5! 5 (5-1) (5-2) (5-3) (5-4)
  • Thus mathematical, the factorial of n is
  • n! n(n-1) (n-2) (n-3) (n-(n-1))

6
Factorial
  • If f is a function, then
  • f(n) n f(n-1)
  • (n-1) f(n-2)
  • (n-2)f(n-3)
  • (n-3)f(n-4)
  • ..
  • .n-(n-2)f(n-(n-1))

7
Factorial
  • Algorithm
  • factorial(n)
  • if n 0
  • return 1
  • otherwise
  • return n factorial(n-1)

8
FIBONACCI NUMBERS
  • Fibonacci number is defined as follows
  • Fib(n) 1 for n 1 n 2
  • Fib(n) Fib(n-1)Fib(n-2) for n gt 2
  • The Fibonacci series
  • 1,1,2,3,5,8,13,21

9
Fibonacci.
  • Algorithm
  • Fib(n)
  • if n 0 or n 2
  • return 0
  • otherwise
  • return Fib(n-1)Fib(n-2)

10
BINOMIAL COEFFICIENT
  • Any one coefficients that arise in the expansion
    of the polynomial (xy)2. Recursively,
  • C(n,k) C(n-1,k-1) C(n-1,k)

11
Binomial
  • function C(n, k) if k 0 or k n
  • return 1
  • otherwise
  • return C(n-1, k-1) C(n-1, k)
  • An Exercise..
  • C(6,5) ?

12
(No Transcript)
13
RECURSIVE BINARY SEARCH
  • Algorithm
  • BinSearch(LB,UB,Arr,Key)
  • If LBgtUB
  • return 0
  • otherwise
  • Mid(LBUB)/2
  • if KeyltArr(Mid)
  • return BinSearch(LB,Mid-1,Arr,Key)
  • othewise
  • if KeygtArr(Mid)
  • return BinSearch(Mid1,UB,Arr,Key)
  • otherwise
  • return Mid

14
Binary Tree Search.
  • IsThere(Tree, item)
  • if isEmpty(tree) then
  • found false
  • else
  • if itemltTree.data then
  • foundisthere(tree.left, item)
  • else
  • if itemgttree.data then
  • foundisthere(tree.right,item)
  • else
  • foundtrue
  • ifend
  • ifend
  • Istherefound

15
Bubble Sort
  • Algorithm
  • BubSort(Arr,Pass,ele)
  • nlength of Arr
  • if ( Passltn-1)
  • if (eleltn-p) then
  • if Arr(ele) gt Arr(ele1)
  • swap(Arr(ele),Arr(ele1))
  • BubSort(Arr,Pass,ele1)
  • BubSort(Arr,Pass1,0)

16
THE END
  • THANK YOU
Write a Comment
User Comments (0)
About PowerShow.com