# 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 I n s e r t i o m f c h a b u l on v g w om p ( L 2 4 )
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 S e ct i o n E x . 1 3 f r t h co d u # m par s

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.

## This note was uploaded on 03/06/2010 for the course CS 9339 taught by Professor Gries during the Fall '09 term at Cornell University (Engineering School).

### Page1 / 71

L26post - Previous Lecture: Divide and conquer strategies...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online