lecture23

lecture23 - CS111: Intro to Computer Science Lecture 23...

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

View Full Document Right Arrow Icon
CS111: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 23 CS111: Intro to Computer CS111: Intro to Computer Science Science Lecture 23 Recursion on lists
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS111: Slides for Prof. Steinberg ʼ s lecture 2 Lecture 23 Recursion Recursion Recursion is a way of looking at a problem EG problem: print pattern like * * * * * * * * * *
Background image of page 2
CS111: Slides for Prof. Steinberg ʼ s lecture 3 Lecture 23 Merge Sort Merge Sort Merge sort: sort a single list using merge to do part of the work Basic idea: Divide unsorted list in half Sort each half Merge the sorted halves together How sort each half? Use merge sort recursively Base case: list of one number is already sorted
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS111: Slides for Prof. Steinberg ʼ s lecture 4 Lecture 23 Example Example -> 7 -> 3 -> 6 ->8 ->1 -> 2 -> 4 ->5 split (any split method works) -> 4 ->1 -> 6 ->7 ->5 ->2 -> 8 -> 3 Sort the short lists recursively -> 1 -> 4 -> 6 -> 7 -> 2 -> 3 -> 5 -> 8 Merge -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
Background image of page 4
CS111: Slides for Prof. Steinberg ʼ s lecture 5 Lecture 23 Merge Sort Merge Sort 7 3 6 8 1 2 4 5 4 1 6 7 5 2 8 3 7 1 6 4 3 2 8 5 7 1 6 4 3 2 8 5 1 7 4 6 2 3 5 8 1 4 6 7 2 3 5 8 1 2 3 4 5 6 7 8
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS111: Slides for Prof. Steinberg ʼ s lecture 6 Lecture 23 Time Cost Time Cost Cost to split list-of-n into two list-of-n/2s O(n) But we split 1 list of n into n/2 time cost n 2 lists of n/2 into n/4 time cost 2*n/2 = n 4 lists of n/4 into n/8 time cost 4*n/4 = n Similar for merging Total time = n + (2 *n/2) + (4*n/4) + …
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

lecture23 - CS111: Intro to Computer Science Lecture 23...

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

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