Wrapping%20up%20Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Wrapping%20up%20Recursion

Description:

Resulting finished pile will be sorted. * Finishing the merge sort All students hand off sorted piles to delegating students, ... College of Natural Science – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 20
Provided by: SystemA401
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Wrapping%20up%20Recursion


1
Wrapping up Recursion
  • Intro to Computer Science
  • CS1510
  • Dr. Sarah Diesburg

2
Recursion
  • Recursive functions are functions that call
    themselves
  • We can create recursive functions by breaking
    them up into two smaller parts

3
1) Call same function with something smaller
  • Recursion is a natural outcome of a divide and
    conquer approach to problem solving.
  • A recursive function defines how to break a
    problem down (divide) and how to reassemble
    (conquer) the sub-solutions into an overall
    solution.

4
2) Base Case
  • Recursion is a process not unlike loop iteration.
  • You must define how long (how many iterations)
    recursion will proceed until it stops.
  • The base case defines this limit.
  • Without the base case, recursion will continue
    infinitely (just like an infinite loop).

5
Some examples of things that can be done
recursively
  • Summation of numbers sum(lower,upper)
  • Exponents - power(base,exp)
  • Reverse a string reverse(string)
  • Merge Sort mergeSort(lyst)

6
Summation
  • Usually with a summation, we have a lower bound
    and upper bound
  • We want to sum from the lower bound to the upper
    bound
  • summation(1,5) would yield
  • 1234515
  • Write a summation function
  • summation(lower,higher)

7
Summation
  • def summation(lower,higher)
  • if lowerhigher
  • return lower
  • else
  • return lower summation(lower1,higher)

8
Power
  • Write a function that takes a base and a power
    and returns the result
  • For example, we know that 24 is 16
  • 2 is the base
  • 4 is the power
  • We break it up as 2 x 2 x 2 x 2 16
  • Same as 2 x 23 16

9
Power
  • def power(base,exp)
  • if (exp0)
  • return 1
  • else
  • return basepower(base,exp-1)

10
Reverse a String
  • def reverse(string)
  • if (len(string))0
  • return ""
  • else
  • return string-1 reverse( string0-1
    )

11
Merge Sort
  • Lets say I have 16 programming assignments, and
    I need to sort them in alphabetical order
  • Im lazy, so I hand off half of the assignments
    to one student to sort, and the other half to
    another student to sort

12
What if everyone hands off the work?
A
16
B
C
13
What if everyone hands off the work?
A
16
B
C
8
8
D
E
F
G
14
What if everyone hands off the work?
A
16
B
C
8
8
D
E
F
G
4
4
4
4
H
2
I
1
15
Merge Sort
  • At some point, the last students will only have 1
    paper.
  • They can then hand those single papers back to
    the student that delegated the work in the first
    place.
  • The delegating student can then sort the two
    piles (of 1) papers by performing a merge.

16
Merge Sort
  • Students I and J hand each of their sorted, 1
    item stacks to H.
  • H performs a merge sort on the two stacks of items

H
1
1
J
I
17
What is a merge?
  • Start with two sorted piles (pile A and pile B)
  • Take the smallest item off the top of pile A or B
    and place it in the finished pile.
  • Repeat the previous step until no more items in
    either pile.
  • Resulting finished pile will be sorted.

18
Finishing the merge sort
  • All students hand off sorted piles to delegating
    students, each of which performs a merge sort
  • Finally the student that started everything (A)
    will merge sort two large piles of items and
    created the final sorted output pile

19
Pseudocode for Merge Sort
  • Has to be some sort of recursive algorithm
  • What is the base case?
  • If you have pile of size 1, it is sorted and you
    are done.
  • What is the recursive case?
  • This is trickier
  • We can merge two sorted piles, but we want the
    two piles to be sorted first
  • Each pile is of size n/2, where n is the total
    number of items we have
Write a Comment
User Comments (0)
About PowerShow.com