Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Quiz 4 will be on next Friday (4/3). It will cover materials from Chapter 4. Check your grades on WebCT and notify me promptly if you see.

Similar presentations


Presentation on theme: "Announcements Quiz 4 will be on next Friday (4/3). It will cover materials from Chapter 4. Check your grades on WebCT and notify me promptly if you see."— Presentation transcript:

1 Announcements Quiz 4 will be on next Friday (4/3). It will cover materials from Chapter 4. Check your grades on WebCT and notify me promptly if you see any inconsistency in your grades (seven homeworks, three quizzes and one midterm). Midterm 2 is in two weeks (4/10). It will cover materials from Chapters 3, 4 and 5 (5.1 and 5.2).

2 Announcements The first midterm will not be curved
Don’t know how to do it Curving exams is overused and so uncreative. Instead, you are allowed to design your own grade distribution. The worksheet will be available for download soon and it is due on the day of the final exam.

3 Merge Sort A recursive sorting algorithm
Procedure mergesort ( L = a1, …., an) if n > 1 then m = floor (n/2) L1 = a1, …., am L2= a m+1, …., an L = merge(mergesort(L1, mergesort(L2)) {L is sorted into elements in nondecreasing order}

4 Merge Sort Procedure merge ( L1, L2 sorted lists) L= empty list
while L1 and L2 are both nonempty begin remove smaller of first element of L1 and L from the list and put it at the right end of L if removal of this elemtn makes one list empty, then remove all elements from the other list and append them to L. {L is sorted into elements in increasing order} L1 L2 L

5 The time complexity if O (n log n) ! (next time).
6 9 5 3 6 9 7 10 1 4 8 2 5 3 8 2 7 10 The time complexity if O (n log n) ! (next time).

6 Analyzing Complexity of Merge Sort
Let’s suppose that n ( the number of elements in the list) is a power of 2 (n=2m). This makes analysis easier. 2m 2m-1 2m-2 Level 1 Level 2

7 At level k, there are 2k lists, each with 2m-k elements.
How many levels do we have? At level k, there are how many mergers ? (2k-1) How many comparisons are need to merge two sorted list of size 2m-k? ( 2m-k + 2m-k -1 ) 2m 2m-1 2m-2 Level 1 Level 2

8 2m 2m-1 2m-2 Level k At level k, the total number of comparisons is 2k-1 (2m-k+1 – 1)

9 Sum all these together : the total number of comparisons is
The dominant term above is n log n. The time complexity is therefore O(n log n)! Questions? Comments?

10 5.1 Counting Counting ? Am I in grade school?
In this Chapter, we will count with our heads, not with our fingers and toes! What problems can be solved with counting? Plenty! For example, How many comparisons does a sorting algorithm perform? How many attempts is needed to crack a code/password? What is the probability of two students in this class having the same birthday?

11

12

13

14

15

16


Download ppt "Announcements Quiz 4 will be on next Friday (4/3). It will cover materials from Chapter 4. Check your grades on WebCT and notify me promptly if you see."

Similar presentations


Ads by Google