Lecture 25: Sequence Algorithms
CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 25: Sequence Algorithms CS 1110 Introduction to Computing - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 25: Sequence Algorithms CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Prelim 2 is graded
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
0 k len(b)
0 k n sequence b <= sorted >= 0 k len(b) 0 k len(b)
3
4
5
6
7
? (unknown values) n >= 0, n_pair = 0 0 n pre: seq s
processed
n_pair = num adjacent pairs in s[0..n-1] 0 n post: seq s ?(unknown) processed 0 k n INV: seq s n_pair = num adjacent pairs in s[0..k-1]
Task: find the maximum of a sequence s
8
Task: find the maximum of a sequence s
big is max of this segment
k n inv: s ?
? (unknown values) n > 0, big = s[0] 0 n pre: s
big is the max of this segment 0 n post: s k = n, big = max of s[0..n-1]
n > 0, big = s[0..k-1]
9
10
Task: Put negative values before nonnegative ones and return the split index
5 -7 2 2 8 -3 9 3
8 2 9 3 5
k = 3
11
Task: Put negative values before nonnegative ones and return the split index
? (unknown) n >= 1 0 n pre: s
0 kà ß j n
? >= 0 < 0
12
5 -7 2 2 8 -3 9 3
8 2 9 3 5
k = 3
< 0 0 k n post: s >= 0
k = j
0 kà ß j n
? >= 0 < 0
13
< 0 0 k n post: s >= 0
k = j