CS 321. Algorithm Analysis & Design
Lecture 2
Sorting
Announcements
Announcements
The first homework will be online this Wednesday
with a deadline of next Wednesday.
Announcements
The first homework will be online this Wednesday
with a deadline of next Wednesday.
Will deal with clarifications on Friday.
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.
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%)
Algorithm
Analysis
Algorithm
Analysis
Running TimeCorrectness
Insertion Sort
Loop Invariants
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.
Initialization
Maintenance
Termination
Initialization
Maintenance
Termination
The statement is true at the beginning.
Initialization
Maintenance
Termination
The statement is true at the beginning.
If the statement was true before the interation,
it is true after the iteration.
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.
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.
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.
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.
Insertion Sort
[1,2,3]
[1,3,2]
[3,1,2]
[2,1,3]
[2,3,1]
[3,2,1]
Best-case running time
Average-case running time
Worst-case running time
Best-case running time
Average-case running time
Worst-case running time
There is some input on which the algorithm enjoys
this running time.
Best-case running time
Average-case running time
Worst-case running time
Work averaged over the space of all inputs.
There is some input on which the algorithm enjoys
this running time.
Best-case running time
Average-case running time
Worst-case running time
Work averaged over the space of all inputs.
No matter what the input, the algorithm will not need more
time than this.
There is some input on which the algorithm enjoys
this running time.
Worst-case running time
No matter what the input, the algorithm will not need more
time than this.
We’ll typically obtain lower and upper bounds
on the worst case running time.
Worst-case is sometimes too
pessimistic an approach.
Real-world inputs almost always
have some structure!
The best case is O(n) and the worst case is O(n2).
Asymptotics
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).
[Chapter 2, KT]
Running times on processors performing a
million high-level operations per second.
[Chapter 2, KT]
Running times on processors performing a
million high-level operations per second.
Allows for coarse running time analysis,
rather than being bogged down with
details.
Ignoring constants can be dangerous in
the real world.
Merge Sort
Induction
Merge Sort
02 - 04 Jan - Sorting (Continued)
02 - 04 Jan - Sorting (Continued)

02 - 04 Jan - Sorting (Continued)