Lecture2013 04 25
Lecture2013 04 25
Previous Lecture:
Recursion
Todays Lecture:
Recursion examples: remove all occurrences of a character in a string, a mesh of triangles Sort algorithm: Insertion Sort See Insight 8.2 for the Bubble Sort algorithm
Announcements:
Algorithms, and functions, can be recursive as well. I.e., a function can call itself. Example: remove all occurrences of a character from a string gc aatc gga c gcaatcggac
Lecture 26 2
Last call to notify us of final exam conflict Project 6 parts A and B due 5/2, Thurs, at 11pm
function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed else . .% return string is just .% the remaining s with char c removed
s = removeChar(c, s(2:length(s))); s = [s(1) removeChar(c, s(2:length(s)))] .
Original problem: remove all the blanks in string s Decompose into two parts: 1. remove blank in s(1) 2. remove blanks in s(2:length(s))
Original problem Decompose Decompose Decompose Decompose
end end
Lecture 26 8
function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end removeChar 2nd call c _ s _ o _ g
s d _ o _ g c _
function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end removeChar 2nd call c _ s _ o _ g
s d _ o _ g c _
[d
[o
[d
[o
Lecture slides
CS1112 Lecture 26
function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end removeChar 2nd call c _ s _ o _ g
s d _ o _ g c _
function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end removeChar 2nd call c _ s _ o _ g
s d _ o _ g c _
[d
[o
[d
o g
o g
[o
d o g
c _ s
Now do the same partitioning (connecting midpts) on each corner (white) triangle to obtain the level-2 partitioning
Lecture 26 30 Lecture 26 31
Lecture slides
CS1112 Lecture 26
Lecture 26
32
Lecture 26
33
The basic operation at each level the triangle is small Dont subdivide and just color it yellow. else Subdivide: Connect the side midpoints; color the interior triangle magenta; apply same process to each outer triangle. end
Lecture 26
35
Lecture 26
36
function MeshTriangle(x,y,L) % x,y are 3-vectors that define the vertices of a triangle. % Draw level-L partitioning. Assume hold is on. if L==0 % Recursion limit reached; no more subdivision required. fill(x,y,'y') % Color this triangle yellow else % Need to subdivide: determine the side midpoints; connect % midpts to get interior triangle; color it magenta.
Key to recursion
Must identify (at least) one base case, the trivially simple case
a = [(x(1)+x(2))/2 (x(2)+x(3))/2 (x(3)+x(1))/2] b= [(y(1)+y(2))/2 (y(2)+y(3))/2 (y(3)+y(1))/2] fill(a,b,'m') . % .Apply the process to the three "corner" triangles... MeshTriangle([x(1) a(1) a(3)], [y(1) b(1) b(3)], L-1) MeshTriangle([x(2) a(2) a(1)], [y(2) b(2) b(1), L-1)
The recursive case(s) must reflect progress towards the base case
E.g., give a shorter vector as the argument to the recursive call see removeChar E.g., ask for a lower level of subdivision in the recursive call see MeshTriangle
end
Lecture 26
56
Lecture slides
CS1112 Lecture 26
Athlete NED Ranomi Kromowidjojo BLR Aliaksandra Herasimenia CHN Tang Yi AUS Melanie Schlanger USA Missy Franklin GBR Francesca Halsall DEN Jeanette Ottesen Gray USA Jessica Hardy
Grade
2012 Olympics Womens 100m Freestyle Splits Time 50m / Reaction 100m / Behind 25.76 0.77 25.22 0.74 25.95 0.71 26.09 0.73 26.22 0.78 25.78 0.70 25.50 0.70 25.69 0.72 27.24 28.16 +0.38 27.49 +0.44 27.38 +0.47 27.42 +0.64 27.88 +0.66 28.25 +0.75 28.33 +1.02 53.00 53.38 53.44 53.47 53.64 53.66 53.75 54.02
Insertion Sort (to be discussed today) Bubble Sort (read Insight 8.2) Merge Sort (to be discussed Thursday) Quick Sort (a variant used by Matlabs built-in sort function) Each has advantages and disadvantages. Some algorithms are faster (time-efficient) while others are memory-efficient Great opportunity for learning how to analyze programs and algorithms!
Lecture 26 62
6.
92.1 90.6
Insertion
one insert process
2 3 6 9 8
Given a sorted array x, insert a number y such that the result is sorted
sorted
2 9 8
3 3 3 3
6 6 6 4
8 8 4 6
9 4 8 8
4 9 9 9
2 2 2
x
Length 1 subvector is sorted Insert x(2): [x(1:2),C,S] = Insert(x(1:2)) Insert x(3): [x(1:3),C,S] = Insert(x(1:3)) Insert x(4): [x(1:4),C,S] = Insert(x(1:4)) Insert x(5): [x(1:5),C,S] = Insert(x(1:5)) Insert x(6): [x(1:6),C,S] = Insert(x(1:6)) InsertionSort.m
Lecture 26 70
Read about Bubble Sort in Insight 8.2 Both algorithms involve the repeated comparison of adjacent values and swaps Find out which algorithm is more efficient on average
Lecture 26
71
Lecture slides