Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 23 Announcements: Announcements: Pass in yesterday’s capsule quiz Pass in yesterday’s capsule quiz Homework 7 electronic.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 23 Announcements: Announcements: Pass in yesterday’s capsule quiz Pass in yesterday’s capsule quiz Homework 7 electronic."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 23 Announcements: Announcements: Pass in yesterday’s capsule quiz Pass in yesterday’s capsule quiz Homework 7 electronic parts: grades in Angel Homework 7 electronic parts: grades in Angel Programming mini-exam Thursday Programming mini-exam Thursday Simulation project feedback in repository Simulation project feedback in repository Any deliverables missing on Thursday will get a 0. Any deliverables missing on Thursday will get a 0.

2 This week: Simulation Project Monday: Monday: Bubble, Selection, and Insertion Sorts Bubble, Selection, and Insertion Sorts File I/O (capsule) File I/O (capsule) Tuesday: Tuesday: Analysis of sorting algorithms Analysis of sorting algorithms Mergesort (capsule) Mergesort (capsule) Thursday: Thursday: Programming mini-exam Programming mini-exam Time to work on project Time to work on project

3 Analyzing Sorts Def: An inversion is any pair of inputs that are out of order: Def: An inversion is any pair of inputs that are out of order: [5,8,3,9,6] has 4 inversions: (5,3), (8,3), (8,6), (9,6) [5,8,3,9,6] has 4 inversions: (5,3), (8,3), (8,6), (9,6) [5,3,8,9,6] has 3 inversions: (5,3), (8,6), (9,6) [5,3,8,9,6] has 3 inversions: (5,3), (8,6), (9,6) Swapping a pair of adjacent elements removes exactly one inversion Swapping a pair of adjacent elements removes exactly one inversion Worst case? Worst case? all n(n-1)/2 pairs are out of order, so n(n-1)/2 swaps. all n(n-1)/2 pairs are out of order, so n(n-1)/2 swaps. Average case: Average case: Consider any array, a, and its reverse, r. Then inv(a) + inv(r) = n(n-1)/2 Consider any array, a, and its reverse, r. Then inv(a) + inv(r) = n(n-1)/2 So on average, n(n-1)/4 inversions. So on average, n(n-1)/4 inversions.

4 Demo Conclusion: if few inversions (almost sorted), then few swaps Conclusion: if few inversions (almost sorted), then few swaps Let’s look at a quick demo of selection, bubble, and insertion sorts… Let’s look at a quick demo of selection, bubble, and insertion sorts… Completely random data Completely random data Nearly sorted data Nearly sorted data

5 How do we beat O(n 2 )? Enter our beloved capsule group… Enter our beloved capsule group…

6 Mergesort Analysis mergesort(a, start, end) mergesort(a, start, end) mid = (start + end) / 2 mid = (start + end) / 2 if (start < end) if (start < end) mergesort(a, start, mid) mergesort(a, start, mid) mergesort(a, mid+1, end) mergesort(a, mid+1, end) merge(a, start, mid, end) merge(a, start, mid, end) Idea: how deep is the recursion? How much work is done in each level of the recursion? Idea: how deep is the recursion? How much work is done in each level of the recursion?

7 Analysis of merge() Merging two sorted arrays of length O(n/2) each is O(n) Merging two sorted arrays of length O(n/2) each is O(n) Why? Why? After each comparison, one element is moved into the sorted array, so there are only n comparisons After each comparison, one element is moved into the sorted array, so there are only n comparisons

8

9 Visual analysis

10 Putting it all together: Should I sort before I search? No. I’m too lazy Initial cost = ______ Initial cost = ______ Need to use sequential search Average cost per search: _____ Average cost per search: _____ Yes. It’s worth the cost Initial cost =_______ Now I can use binary search Average cost per search: _____ If we had an array with 1M elements and anticipate searching it 1M times, which approach is faster?

11 Putting it all together: Should I sort before I search? No. I’m too lazy Initial cost = 0 Initial cost = 0 Need to use sequential search Average cost per search: n/2 Average cost per search: n/2 Yes. It’s worth the cost Initial cost = nlog(n) Now I can use binary search Average cost per search: log(n) If we had an array with 1M elements and anticipate searching it 1M times, which approach is faster? sort!


Download ppt "CSSE221: Software Dev. Honors Day 23 Announcements: Announcements: Pass in yesterday’s capsule quiz Pass in yesterday’s capsule quiz Homework 7 electronic."

Similar presentations


Ads by Google