Unformatted text preview: O(n) algorithm (which requires at most n 3 comparisons. ) Initially we set Ml to be the larger of al, a2 and M2 the lesser. Then, in tum, we compare M2 with each ai. When ai > M 2, we check whether or not ai is also larger than M l . Eventually, Ml is the largest and M2 the second largest integer. Step 1: if al > a2, set Ml = al and M2 = al; else set Ml = a2 and M2 = al. Step 2: ifn > 2, fori = 3 to n, ifai > M2 if ai > Mlo replace Ml by ai and M2 by M l ; else replace M2 by ai. Step 3: output Ml and M 2. 5. Step 1: Let M = al and m = al. Step 2: for i = 2 to n, • if ai > M, replace M by ai; • if ai < m, replace m by ai; Step 3: Output ''The largest number is M and the smallest is m." 6. Step 1: Set n = O. Step 2: If x > 0 while x> n replace n by n + 1; output n  1; else while x > n replace n by n + 1; output n 7. (a) 1 k + 2k + ... + n k :::; n k + n k + ... n k = n(nk) = nk+l, so the result follows with c = 1 and no = 1....
View
Full Document
 Summer '10
 any
 Graph Theory, Addition, TEP

Click to edit the document details