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

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

View Full Document Right Arrow Icon
CS 173: Discrete Mathematical Structures Cinda Heeren heeren@cs.uiuc.edu Siebel Center, rm 2213 Office Hours: W 9:30-11:30a
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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.
Background image of page 6
Cs173 - Spring 2004 CS173 Algorithms Algorithm BUBBLE Input: x 1 , x 2 , …, x n , an array of numbers Output: ??
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/15/2008 for the course CS 173 taught by Professor Fleck@shaffer during the Spring '08 term at University of Illinois at Urbana–Champaign.

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 Right Arrow Icon
Ask a homework question - tutors are online