{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# lec0302 - CS 173 Discrete Mathematical Structures Cinda...

This preview shows pages 1–8. Sign up to view the full content.

CS 173: Discrete Mathematical Structures Cinda Heeren [email protected] Siebel Center, rm 2213 Office Hours: W 9:30-11:30a

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cs173 - Spring 2004 CS 173 Announcements Homework 6 available. Due 03/05, 8a. Midterm 1 returned Thursday for sure. New section, Thursday 6-7p in Siebel 1129 has been CANCELLED!!! Exam stats: Participants: 98 Average: 69.2 Median: 71.0 Maximum: 100.0 Standard Deviation: 16.41 midterm 1 distribution 0 5 10 15 20 25 30 21-30 31-40 41-50 51-60 61-70 71-80 81-90 91-100 score range coun
Cs173 - Spring 2004 CS173 Algorithms Algorithm MAX Input: x 1 , x 2 , …, x n , an array of numbers Output: y, the maximum of x 1 , x 2 , …, x n for j = 1 to n-1 if x j > x j+1 then temp = x j+1 x j+1 = x j x j = temp Complexity is O(n) 3 3 3 2 2 x 2 4 1 2 final 1 4 2 j = 3 1 4 2 j = 2 1 4 3 j = 1 1 4 3 input x 4 x 3 x 1 vars

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cs173 - Spring 2004 CS173 Algorithms Algorithm MAX Input: x 1 , x 2 , …, x n , an array of numbers Output: y, the maximum of x 1 , x 2 , …, x n for j = 1 to n-1 if x j > x j+1 then temp = x j+1 x j+1 = x j x j = temp Can we PROVE this algorithm works? The proof of correctness for an iterative algorithm is typically done via induction.
Cs173 - Spring 2004 CS173 Algorithms Algorithm MAX Input: x 1 , x 2 , …, x n , an array of numbers Output: y, the maximum of x 1 , x 2 , …, x n for j = 1 to n-1 if x j > x j+1 then temp = x j+1 x j+1 = x j x j = temp Prove that x j+1 = max{x 1 , x 2 , …, x j+1 } after the j th iteration of the loop. Note that the last iteration (j = n-1) gives the result we really want.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cs173 - Spring 2004 CS173 Algorithms Algorithm MAX Input: x 1 , x 2 , …, x n , an array of numbers Output: x n , the maximum of x 1 , x 2 , …, x n for j = 1 to n-1 if x j > x j+1 then temp = x j+1 x j+1 = x j x j = temp Base case (j = 0 th iteration): x 1 = max{x 1 } IH: assume assertion holds for j = n th iteration. On n+1 st iteration, x n+1 is compared with x n+2 and max is swapped into x n+2 . But x n+2 = max{x n+1 , x n+2 } = max{ x 1 , x 2 , …, x n+1 , x n+2 } by IH.
Cs173 - Spring 2004 CS173 Algorithms Algorithm BUBBLE Input: x 1 , x 2 , …, x n , an array of numbers Output: ??

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 26

lec0302 - CS 173 Discrete Mathematical Structures Cinda...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online