L26post

L26post - Previous Lecture Divide and conquer strategies...

This preview shows pages 1–14. Sign up to view the full content.

Previous Lecture: “Divide and conquer” strategies Binary search Merge sort Today’s Lecture: Some efficiency considerations “Divide and conquer” strategies (cont’d)—recursion Merge sort Removing a character (e.g., the blank) from a string Tiling (subdividing) a triangle, e.g., Sierpinski Triangle Announcements Discussion this week in the computer lab (UP B7) Project 7 due Thursday at 11pm CS1112 final will be 12/14 (Mon) 7pm in Barton West

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 6 Merge sort is a “divide-and-conquer” strategy J N R C P D F L A Q B K M G H E
Dec 1, 2009 Lecture 26 7 function y = mergeSort(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSort(x(1:m)); yR = mergeSort(x(m+1:n)); y = merge(yL,yR); end

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 8 function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end
Dec 1, 2009 Lecture 26 9 function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 10 function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end
Dec 1, 2009 Lecture 26 13 How do merge sort, insertion sort, and bubble sort compare? Insertion sort and bubble sort are similar Both involve a series of comparisons and swaps Both involve nested loops Merge sort uses recursion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 14 function x = insertSort(x) % Sort vector x in ascending order with insertion sort n = length(x); for i= 1:n-1 % Sort x(1:i+1) given that x(1:i) is sorted j= i; need2swap= x(j+1) < x(j); while need2swap % swap x(j+1) and x(j) temp= x(j); x(j)= x(j+1); x(j+1)= temp; j= j-1; need2swap= j>0 && x(j+1)<x(j); end end In sertion sort is m o r e eff ic ient than bu bble sort on av e r age —fewer compa risons ( Lecture 24)
Dec 1, 2009 Lecture 26 16 How do merge sort and insertion sort compare? Insertion sort: (worst case) makes i comparisons to insert an element in a sorted array of i elements. For an array of length N: 1+2+…+(N-1) = N(N-1)/2, say N 2 for big N Merge sort:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 17 function y = mergeSort(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSort(x(1:m)); yR = mergeSort(x(m+1:n)); y = merge(yL,yR); end All the comparisons between vector values are done in merge
Dec 1, 2009 Lecture 26 18 function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny if x(ix) <= y(iy) z(iz)= x(iy); ix=ix+1; iz=iz+1; else z(iz)= y(iy); iy=iy+1; iz=iz+1; end end while ix<=nx % copy remaining x-values z(iz)= x(ix); ix=ix+1; iz=iz+1; end while iy<=ny % copy remaining y-values z(iz)= y(iy); iy=iy+1; iz=iz+1; end See Sect i o n Ex . 1 3 for the code to co unt the # of co o mparisons

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dec 1, 2009 Lecture 26 19 Merge sort: log 2 (N) “levels”; N comparisons each level J N R C P D F L A Q B K M G H E
Dec 1, 2009 Lecture 26 20 How do merge sort and insertion sort compare?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern