02 - 04 Jan - Sorting (Continued)
-
Upload
neeldhara-misra -
Category
Education
-
view
135 -
download
2
Transcript of 02 - 04 Jan - Sorting (Continued)
![Page 1: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/1.jpg)
CS 321. Algorithm Analysis & Design Lecture 2
Sorting
![Page 2: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/2.jpg)
Announcements
![Page 3: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/3.jpg)
Announcements
The first homework will be online this Wednesday with a deadline of next Wednesday.
![Page 4: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/4.jpg)
Announcements
The first homework will be online this Wednesday with a deadline of next Wednesday.
Will deal with clarifications on Friday.
![Page 5: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/5.jpg)
Announcements
The first homework will be online this Wednesday with a deadline of next Wednesday.
Please take the informal survey on the website, or send me an email with the subject [CS321].
Will deal with clarifications on Friday.
![Page 6: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/6.jpg)
Graded Work
Google Code Jam Qualifiers (20%)
Self-assessment quizzes via MCQs online (25%)
Assignments on the Hacker Rank Platform (25%)
Mid-sem and End-sem Exams (30%)
![Page 7: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/7.jpg)
Algorithm Analysis
![Page 8: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/8.jpg)
Algorithm Analysis
Running TimeCorrectness
![Page 9: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/9.jpg)
Insertion Sort
![Page 10: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/10.jpg)
Loop Invariants
![Page 11: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/11.jpg)
![Page 12: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/12.jpg)
At the start of each iteration of the for loop of lines 1–8,the subarray A[1 . . j − 1] consists of the elements
originally in A[1 . . j − 1] but in sorted order.
![Page 13: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/13.jpg)
Initialization
Maintenance
Termination
![Page 14: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/14.jpg)
Initialization
Maintenance
Termination
The statement is true at the beginning.
![Page 15: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/15.jpg)
Initialization
Maintenance
Termination
The statement is true at the beginning.
If the statement was true before the interation,it is true after the iteration.
![Page 16: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/16.jpg)
Initialization
Maintenance
Termination
The statement is true at the beginning.
If the statement was true before the interation,it is true after the iteration.
When the loop ends, the invariant gives us something useful.
![Page 17: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/17.jpg)
At the start of each iteration of the for loop of lines 1–8, the subarray A[1 . . j − 1] consists of the elements
originally in A[1 . . j − 1] but in sorted order.
![Page 18: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/18.jpg)
At the start of each iteration of the for loop of lines 1–8, the subarray A[1 . . j − 1] consists of the elements
originally in A[1 . . j − 1] but in sorted order.
![Page 19: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/19.jpg)
At the start of each iteration of the for loop of lines 1–8, the subarray A[1 . . j − 1] consists of the elements
originally in A[1 . . j − 1] but in sorted order.
![Page 20: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/20.jpg)
Insertion Sort
![Page 21: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/21.jpg)
![Page 22: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/22.jpg)
![Page 23: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/23.jpg)
![Page 24: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/24.jpg)
![Page 25: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/25.jpg)
[1,2,3]
[1,3,2]
[3,1,2]
[2,1,3]
[2,3,1]
[3,2,1]
![Page 26: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/26.jpg)
Best-case running time
Average-case running time
Worst-case running time
![Page 27: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/27.jpg)
Best-case running time
Average-case running time
Worst-case running time
There is some input on which the algorithm enjoysthis running time.
![Page 28: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/28.jpg)
Best-case running time
Average-case running time
Worst-case running time
Work averaged over the space of al l inputs.
There is some input on which the algorithm enjoysthis running time.
![Page 29: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/29.jpg)
Best-case running time
Average-case running time
Worst-case running time
Work averaged over the space of al l inputs.
No matter what the input, the algorithm wil l not need moretime than this.
There is some input on which the algorithm enjoysthis running time.
![Page 30: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/30.jpg)
Worst-case running time
No matter what the input, the algorithm wil l not need more time than this.
We’ll typically obtain lower and upper bounds on the worst case running time.
![Page 31: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/31.jpg)
Worst-case is sometimes too pessimistic an approach.
Real-world inputs almost always have some structure!
![Page 32: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/32.jpg)
The best case is O(n) and the worst case is O(n2).
![Page 33: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/33.jpg)
Asymptotics
![Page 34: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/34.jpg)
T(n) is O(f(n))
There exist constants c > 0 and no> 0 so that for all n >no, we
have T(n) ≤ c f(n).
![Page 35: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/35.jpg)
[Chapter 2, KT]
Running times on processors performing a million high-level operations per second.
![Page 36: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/36.jpg)
[Chapter 2, KT]
Running times on processors performing a million high-level operations per second.
![Page 37: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/37.jpg)
Allows for coarse running time analysis, rather than being bogged down with
details.
Ignoring constants can be dangerous in the real world.
![Page 38: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/38.jpg)
Merge Sort
![Page 39: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/39.jpg)
Induction
![Page 40: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/40.jpg)
![Page 41: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/41.jpg)
![Page 42: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/42.jpg)
![Page 43: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/43.jpg)
![Page 44: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/44.jpg)
![Page 45: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/45.jpg)
![Page 46: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/46.jpg)
![Page 47: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/47.jpg)
Merge Sort
![Page 48: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/48.jpg)
![Page 49: 02 - 04 Jan - Sorting (Continued)](https://reader034.fdocuments.us/reader034/viewer/2022051706/58efcbd51a28ab85148b458f/html5/thumbnails/49.jpg)