Sie sind auf Seite 1von 4

CSCI 303 Homework 1

Problem 1 (2.1-1): Using Figure 2.2 as a model, illustrate the operation of Insertion-Sort on the array A = 31, 41, 59, 26, 41, 58 . Solution 1:

31 41 59 26 41 58

31 41 59 26 41 58

31 41 59 26 41 58

26 31 41 59 41 58

26 31 41 41 59 58

26 31 41 41 58 59

Problem 2 (2.2-1): Express the function n3 /1000 100n2 100n + 3 in terms of -notation. Solution 2: n3 /1000 100n2 100n + 3 = (n3 )

Problem 3 (Derived from 2.2-2): Consider sorting n numbers stored in array A by rst nding the smallest element of A and exchanging it with the element in A[1]. Then nd the second smallest element of A, and exchange it with A[2]. Continue in this manner for the rst n 1 elements of A. Write pseudocode for this algorithm, which is known as Selection-Sort. Give the worst-case running times of selection sort in -notation. Solution 3: Selection-Sort(A) for i 1 to length[A] do min-value A[i] min-index = i for j = i + 1 to length[A] do if A[j] min-value min-value = A[j] min-index = j A[i] A[min-index] The worst-case running time of Selection-Sort is (n2 ).

Problem 4 (2.3.1): Using Figure 2.4 as a model, illustrate the operation of merge sort on the array A = 3, 41, 52, 26, 38, 57, 9, 49 . Solution 4:

Sorted Sequence 26 38 41 49

52

57

26

41

52

38

49

57

41

26

52

38

57

49

41

52

26

38

57

49

Initial Sequence

Problem 5 (Derived from 1.2-2): Suppose we are comparing two sorting algorithms. Suppose that for all inputs of size n, the rst algorithm runs in 8n2 seconds, while the second algorithm runs in 64n lg n seconds. For which values of n does the rst algorithm beat the second algorithm? Solution 5: The rst algorithm beats the second algorithm if 8n2 < 64n lg n. For this to happen, n < 8 lg n. This is true for 2 n 43. Problem 6 (1.2-3): What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n ? Solution 6: n = 15

Problem 7 (1.2-3): Assume that a new Intel processor can execute 1015 operations per second. You have two algorithms that test whether a number is prime or not. The rst algorithm uses 100n2 operations for a number with n decimal digits. The second uses 2n operations for a number with n decimal digits. Using the rst algorithm, how many seconds would the Intel processor take to determine whether a 1000 decimal digit number is prime? Using the second algorithm, how many seconds would the Intel processor take to determine whether a 1000 decimal digit number is prime? Solution 7: The rst algorithm would take 107 seconds (100 nanoseconds). The second algorithm would take 10286 seconds. For comparison, the universe is about 4.33 1017 seconds old, so it would take approximately 2.5 10268 times the age of the universe to solve the problem using the second algorithm. Problem 8 (1-1 Comparison of running times): For each function f (n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f (n) microseconds. 1 second 1 minute 1 hour 1 day 1 month 1 year 1 century lg n n n n lg n n2 n3 2n n! Solution 8: 1 second lg n n n n lg n n2 n3 2n n!
6 210

1 minute
7 2610

1 hour
9 23.610

1 day
10 28.6410

1 month
12 22.6310

1 year
13 23.1610

1 century 23.1610 9.96 1030 3.16 1015 6.87 1013 56175382 146677 51 17
15

1012 106 62746 1000 100 19 9

3.6 1015 1.3 1019 7.46 1021 6.92 1024 9.96 1024 6 107 3.6 109 8.64 1010 2.63 1012 3.16 1013 2801417 1.33 108 2.76 109 7.29 1010 7.98 1011 7745 60000 293938 1621643 5617538 391 1532 4420 13802 31600 25 31 36 41 44 11 12 13 15 16

Problem 9 (Derived from 3.1-2): Show that for all numbers a and b, (n + a)b = (nb ). Solution 9:
b b

(n + a) =
k=0 b

b bk k n a k

(The binomial formula)

= n + bnb1 k + + bnk b1 + k b = (nb )

Problem 10 (3.1-4): Is 2n+1 = O(2n )? Is 22n = O(2n )? Solution 10: 2n+1 = 2 2n 22n = 2n 2n so so 2n+1 = O(2n ) 22n = O(2n )

Problem 11 (Not in book): In the table below, write O if f (n) = O(g(n)), if f (n) = (g(n)), and if f (n) = (g(n)). g(n) f (n) 5n + 3 3.14 108 8n2 lg4 n + n3 2n 5n3 + 4n2 lg n + 3n 12n + lg8 n 5n + 3 3.14 108 8n2 lg4 n + n3 2n 5n3 + 4n2 lg n + 3n 12n + lg8 n Solution 11: g(n) f (n) 5n + 3 3.14 108 8n2 lg4 n + n3 2n 5n3 + 4n2 lg n + 3n 12n + lg8 n 5n + 3 O 8 3.14 10 2 lg4 n + n3 8n O O O n 2 O O O O O 3 + 4n2 lg n + 3n 5n O O O O 8 12n + lg n O

Das könnte Ihnen auch gefallen