L26post - Previous Lecture Divide and conquer strategies...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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)
Image of page 8
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:
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
Dec 1, 2009 Lecture 26 20 How do merge sort and insertion sort compare?
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern