02 - 04 Jan - Sorting (Continued)

49
CS 321. Algorithm Analysis & Design Lecture 2 Sorting

Transcript of 02 - 04 Jan - Sorting (Continued)

Page 1: 02 - 04 Jan - Sorting (Continued)

CS 321. Algorithm Analysis & Design Lecture 2

Sorting

Page 2: 02 - 04 Jan - Sorting (Continued)

Announcements

Page 3: 02 - 04 Jan - Sorting (Continued)

Announcements

The first homework will be online this Wednesday with a deadline of next Wednesday.

Page 4: 02 - 04 Jan - Sorting (Continued)

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)

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)

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)

Algorithm Analysis

Page 8: 02 - 04 Jan - Sorting (Continued)

Algorithm Analysis

Running TimeCorrectness

Page 9: 02 - 04 Jan - Sorting (Continued)

Insertion Sort

Page 10: 02 - 04 Jan - Sorting (Continued)

Loop Invariants

Page 11: 02 - 04 Jan - Sorting (Continued)
Page 12: 02 - 04 Jan - Sorting (Continued)

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)

Initialization

Maintenance

Termination

Page 14: 02 - 04 Jan - Sorting (Continued)

Initialization

Maintenance

Termination

The statement is true at the beginning.

Page 15: 02 - 04 Jan - Sorting (Continued)

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)

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)

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)

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)

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)

Insertion Sort

Page 21: 02 - 04 Jan - Sorting (Continued)
Page 22: 02 - 04 Jan - Sorting (Continued)
Page 23: 02 - 04 Jan - Sorting (Continued)
Page 24: 02 - 04 Jan - Sorting (Continued)
Page 25: 02 - 04 Jan - Sorting (Continued)

[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)

Best-case running time

Average-case running time

Worst-case running time

Page 27: 02 - 04 Jan - Sorting (Continued)

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)

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)

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)

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)

Worst-case is sometimes too pessimistic an approach.

Real-world inputs almost always have some structure!

Page 32: 02 - 04 Jan - Sorting (Continued)

The best case is O(n) and the worst case is O(n2).

Page 33: 02 - 04 Jan - Sorting (Continued)

Asymptotics

Page 34: 02 - 04 Jan - Sorting (Continued)

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)

[Chapter 2, KT]

Running times on processors performing a million high-level operations per second.

Page 36: 02 - 04 Jan - Sorting (Continued)

[Chapter 2, KT]

Running times on processors performing a million high-level operations per second.

Page 37: 02 - 04 Jan - Sorting (Continued)

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)

Merge Sort

Page 39: 02 - 04 Jan - Sorting (Continued)

Induction

Page 40: 02 - 04 Jan - Sorting (Continued)
Page 41: 02 - 04 Jan - Sorting (Continued)
Page 42: 02 - 04 Jan - Sorting (Continued)
Page 43: 02 - 04 Jan - Sorting (Continued)
Page 44: 02 - 04 Jan - Sorting (Continued)
Page 45: 02 - 04 Jan - Sorting (Continued)
Page 46: 02 - 04 Jan - Sorting (Continued)
Page 47: 02 - 04 Jan - Sorting (Continued)

Merge Sort

Page 48: 02 - 04 Jan - Sorting (Continued)
Page 49: 02 - 04 Jan - Sorting (Continued)