0% found this document useful (0 votes)
57 views4 pages

Lecture2013 04 25

The document summarizes key points from CS1112 Lecture 26: 1) The lecture covered recursion and insertion sort algorithms. Recursion was demonstrated using examples like the Fibonacci sequence and removing characters from a string. 2) Insertion sort was explained as sorting a list by inserting each element into the sorted portion of the list. A recursive mesh generation algorithm was provided as another example of recursion. 3) Announcements included an upcoming project deadline and notifying the instructor about exam conflicts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views4 pages

Lecture2013 04 25

The document summarizes key points from CS1112 Lecture 26: 1) The lecture covered recursion and insertion sort algorithms. Recursion was demonstrated using examples like the Fibonacci sequence and removing characters from a string. 2) Insertion sort was explained as sorting a list by inserting each element into the sorted portion of the list. A recursive mesh generation algorithm was provided as another example of recursion. 3) Announcements included an upcoming project deadline and notifying the instructor about exam conflicts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS1112 Lecture 26

Previous Lecture:

Recursion

OOP: Overriding methods Introduction to recursion

The Fibonacci sequence is defined recursively:


F(1)=1, F(2)=1, F(3)= F(1) + F(2) = 2 F(4)= F(2) + F(3) = 3

Todays Lecture:

F(k) = F(k-2) + F(k-1)

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

It is defined in terms of itself; its definition invokes itself.

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

Example: removing all occurrences of a character Can solve using recursion


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

Decompose into 2 parts

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 _

removeChar 1st call c _ s d _ o _ g

removeChar 3rd call c _ s o _ g

removeChar 1st call c _ s d _ o _ g

removeChar 3rd call c _ s o _ g

removeChar 4th call c _ s _ g

[d

[o

[d

[o

removeChar 6th call c _ s

removeChar 5th call c _ s g

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 _

removeChar 1st call c _ s d _ o _ g

removeChar 3rd call c _ s o _ g

removeChar 4th call c _ s _ g

removeChar 1st call c _ s d _ o _ g

removeChar 3rd call c _ s o _ g

removeChar 4th call c _ s _ g

[d

[o

[d

o g

o g

[o

removeChar 6th call c _ s

removeChar 5th call c _ s g

removeChar 6th call

removeChar 5th call c _ s g

d o g

c _ s

Divide-and-conquer methods, such as recursion, is useful in geometric situations

Why is mesh generation a divide-&-conquer process?

Lets draw this graphic

Chop a region up into triangles with smaller triangles in areas of interest

Recursive mesh generation


Lecture 26 26 Lecture 26 29

Start with a triangle

A level-1 partition of the triangle


(obtained by connecting the midpoints of the sides of the original triangle)

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

The level-2 partition of the triangle

The level-3 partition of the triangle

Lecture 26

32

Lecture 26

33

The level-4 partition of the triangle if

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

No recursion is done in this 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

Sorting data allows us to search more easily


Rank 1 2 3 4. 5.
Name Jorge Ahn Oluban Chi Minale Bell Score

There are many algorithms for sorting


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

91.5 7. 88.9 8. 88.1 87 3

The Insertion Process

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

one insert process


2 3 6 8 9

2 2 2

Compare adjacent components: DONE! No more swaps.

See Insert.m for the insert process


Lecture 26 63 Lecture 26 69

Sort vector x using the Insertion Sort algorithm


Need to start with a sorted subvector. How do you find one?

Insertion Sort vs. Bubble Sort


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

You might also like