L25post - Previous Lecture: Linear Search Bubble Sort,...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
± Previous Lecture: ± Linear Search ± Bubble Sort, Insertion Sort ± Today’s Lecture: ± “Divide and conquer” strategies ± Binary search ± Merge sort ± Recursion ± Announcements: ± Section this week in classrooms, and attendance is not required . Go if you can but for sure do the exercise. ± P7 posted. Due Dec 3rd.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Nov 24, 2009 Lecture 25 3 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) end end S o rt i n g s d e - p l a c . m t r u f B b
Background image of page 2
Nov 24, 2009 Lecture 25 4 An ordered (sorted) list The Manhattan phone book has 1,000,000+ entries. How is it possible to locate a name by examining just a tiny , tiny fraction of those entries?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Nov 24, 2009 Lecture 25 5 % Linear Search % f is index of first occurrence of value x in vector v. % f is -1 if x not found. k= 1; while k<=length(v) && v(k)~=x k= k + 1; end if k>length(v) f= -1; % signal for x not found else f= k; end 12 15 35 33 42 45 v x 31 S ea r c h i n g a u s ort ed l t
Background image of page 4
Nov 24, 2009 Lecture 25 6 Key idea of “phone book search”: repeated halving To find the page containing Pat Reed ’s number… while (Phone book is longer than 1 page) Open to the middle page. if Reed ” comes before the first entry, Rip and throw away the 2 nd half. else Rip and throw away the 1 st half. end end
Background image of page 5

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

View Full DocumentRight Arrow Icon
Nov 24, 2009 Lecture 25 7 What happens to the phone book length? Original: 3000 pages After 1 rip: 1500 pages After 2 rips: 750 pages After 3 rips: 375 pages After 4 rips: 188 pages After 5 rips: 94 pages : After 12 rips: 1 page
Background image of page 6
Nov 24, 2009 Lecture 25 8 Binary Search Repeatedly halving the size of the “search space” is the main idea behind the method of binary search . An item in a sorted array of length n can be located with just log 2 n comparisons.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Nov 24, 2009 Lecture 25 9 % Linear Search % f is index of first occurrence of value x in vector v. % f is -1 if x not found. k= 1; while k<=length(v) && v(k)~=x k= k + 1; end if k>length(v) f= -1; % signal for x not found else f= k; end n co mp a r i s o e d wo t ca . = l g h ( v )
Background image of page 8
Lecture 25 10 Binary Search Repeatedly halving the size of the “search space” is the main idea behind the method of binary search . An item in a sorted array of length
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 58

L25post - Previous Lecture: Linear Search Bubble Sort,...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online