{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

3.3 (evens)

# 3.3 (evens) - Section 3.3 Complexity of Algorithms 67...

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

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.

Unformatted text preview: Section 3.3 Complexity of Algorithms 67 SECTION 3.3 Complexity of Algorithms 2. 10. 12. 14. . . . k . If we successively square it times, then we have computed 3:2 . Thus we can compute .r We can sort the first four elements (which we assume are distinct integers) by the comparisons shown. (After the first three steps, the smallest element is deﬁnitely in the ﬁrst position; after the next two steps, the next smallest element is in the second position.) Since only six comparisons are used, regardless of the length of the list, this algorithm has complexity 0(1). procedure sort four(a1,a2, . . . ,e" : distinct integers) if (14 < :13 then interchange (i4 and a3 ' if 03 < (12 then interchange a3 and (12 if (12 < or then interchange (1.2 and a] if (14 < a3 then interchange a4 and :13 if (13 < (:2 then interchange a3 and 02 if an; < (:3 then interchange a4 and a3 2*. with only A" multiplications, rather than the 2" — 1 multiplications that the naive algorithm would require, so this method is much more efﬁcient. . a) By the way that S — 1 is defined, it is clear that SA (8‘ i '1) is the same as 5' except that the rightmost 1 bit has been changed to a 0. Thus we add 1 to count for every one bit (since we stop as soon as S : 0, i.e., as soon as 3 consists ofjust 0 bits). b) Obviously the number of bitwise AND operations is equal to the ﬁnal value of count, i.e., the number of one bits in S. . a) Initially 11:23. For i = 1 we set 3,: to 32 +1 : 7. For i = 2 we set y to 7-2 +1 2 15, and we are done. b) There is one multiplication and one addition for each of the n passes through the loop, so there are n multiplications and n. additions in all. “’0 are asked to compute (2n.2 -+ 2”) - 10‘9 for each of these values of n. \Vhen appropriate, we change the units from seconds to some larger unit of time. a) 1.224 x 10’6 seconds b) approximately 1.05 x 10—3 seconds c) approximately 1.13 X 106 seconds, which. is about 13 days (nonstop) (1) approximately 1.27 x 10'21 seconds, which is about 4 x 1013 years (nonstop) a) The number of comparisons does not depend on the values of (11 through an. Exactly 2?: w 1 comparisons are used, as was determined in Example 1. In other words, the best case performance is 0(a). b) In the best case .1; 2 a]. We saw in Example. 4 that three comparisons are used in that case. The best case perforn'ianee, then, is 0(1). c) It is hard to give an exact answer, since it depends on the binary rz—zpresentation of the number 12, among other things. In any case, the best case performance is really not much (liﬂ‘crent from the worst case perfor— rnance, namely O(logn), since the list is essentially cut in half at each iteration, and the algorithm does not stop until the list has only one element left in it. a) In order to find the maximum element of a list of n elements, we need to make at least it — 1 comparisons, one to rule out each of the other elements. Since Algorithm 1 in Section 3.1 used just this number (not counting bookkeeping), it is optimal. b) Linear search is not optimal, since we found that binary search was more efﬁcient. This assumes that we can be given the list already sorted into increasing order. 68 16. 18. 20. 22. 24. 26. 28. Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices We will count comparisons of elements in the list to .’L‘. (This ignores comparisons of subscripts, but since we are only interested in a big—0 analysis, no harm is done.) Furthern‘iore, we will assume that the number of elements in the list is a power of 4, say n. = 4k . Just as in the case of binary search, we need to determine the maximum number of times the while loop is iterated. Each pass through the loop cuts the number of elements still being considered (those whose subscripts are from 1' to j) by a factor of 4. Therefore after I: iterations, the active portion of the list will have length 1; that is, we will have i 2 j. The loop terminates at this point. Now each iteration of the loop requires two comparisons in the worst case (one with em and one with either a; or a” ). Three more comparisons are needed at the end. Therefore the number of comparisons is 2k. + 3, which is 0(Fr). Bnt ff : log4n, which is O(log n) since logarithms to different bases differ only by multiplicative constants, so the time complexity of this algorithm (in all cases, not just the worst case) is O(log n) . The algorithm we gave for hurling all the modes essentially just goes through the list once, doing a little bookkeeping at each step. In particular, between any two successive executions of the statement 2' :: i +7 1 there are at most abOut eight operations (such as comparing an unit with modecormt, or l'einitializing value). Therefore at most about 87?. steps are done in all, so the time complexity in all cases is 0(-n). The algorithm we gave is clearly of linear time complexity, i.e., O{n), since we were able to keep updating the sum of previous terms, rather than recomputing it each time. This applies in all cases, not just the worst C3564 The algorithm read through the list once and did a bounded amount of work on each term. Looked at another way, only a bounded amount of work was done between increments of j in the algorithm given in the solution. Thus the complexity is ()(n). It takes 717 1 comparisons to ﬁnd the least element in the list, then it- 2 comparisons to ﬁnd the least element among the remaining elements, and so on. Thus the total number of comparisons is (-n.—1)+(-n.—2)+- - -+2+1 : n,(-n. — 1)/2, which is 0(n2). Each iteration (determining whether we can use a coin of a given denomination) takes a bounded amount of time, and there are at most It iterations, since each iteration decreases the number of cents remaining. Therefore there are 0(n} comparisons. a) The bubble sort algorithm uses about 112/2 comparisons for a list of length n, and (2102/2 : 2n2 comparisons for a list of length 271. Therefore the number of comparisons goes up by a factor of 4. b) The analysis is the same as for bubble sort. c) The analysis is the same as for bubble sort. d) The binary insertion sort algorithm uses about Cn logn. comparisons for a list of length n, where C is a constant. Therefore it uses about C - 2n log 2n = C - 2n log2 + C - 2-nlogn : C - 211+ C - 2n log 71 comparisons for a list of length 211.. Therefore the number of comparisons increases by about a factor of 2 (for large -n, the ﬁrst term is small compared to the second and can be ignored). ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

3.3 (evens) - Section 3.3 Complexity of Algorithms 67...

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

View Full Document
Ask a homework question - tutors are online