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 = h31, 41, 59, 26, 41, 58i. Solution 1: 31 26 58 41 59 41 31 26 58 41 59 41 31 26 58 41 59 41 26 58 41 59 41 31 26 58 41 59 41 31 26 58 41 59 41 31 Problem 2 (2.2-1): Express the function n 3 /1000 - 100n 2 - 100n + 3 in terms of Θ-notation. Solution 2: n 3 /1000 - 100n 2 - 100n + 3 = Θ(n 3 ) Problem 3 (Derived from 2.2-2): Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first 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 Θ(n 2 ).

Algorithms chapter 1 2 and 3 solutions

Embed Size (px)

DESCRIPTION

Algorithms chapter 1 2 and 3 selected solutions

Citation preview

Page 1: Algorithms chapter 1 2 and 3 solutions

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 26 58415941 31 26 58415941 31 26 58415941

26 5841594131 26 5841 594131 26 5841 594131

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 first finding the smallest element of A andexchanging it with the element in A[1]. Then find the second smallest element of A, and exchangeit with A[2]. Continue in this manner for the first n− 1 elements of A. Write pseudocode for thisalgorithm, which is known as Selection-Sort. Give the worst-case running times of selectionsort in Θ-notation.

Solution 3:Selection-Sort(A)

for i← 1 to length[A]do min-value← A[i]

min-index = ifor j = i + 1 to length[A]

do if A[j] ≤ min-valuemin-value = A[j]min-index = j

A[i]↔ A[min-index]

The worst-case running time of Selection-Sort is Θ(n2).

Page 2: Algorithms chapter 1 2 and 3 solutions

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:

52 9573826413 49

26 52 9 4938 573 41

3 26 41 52 9 38 49 57

3 9 26 38 41 49 52 57

Initial Sequence

Sorted Sequence

Problem 5 (Derived from 1.2-2):Suppose we are comparing two sorting algorithms. Suppose that for all inputs of size n, the firstalgorithm runs in 8n2 seconds, while the second algorithm runs in 64n lg n seconds. For whichvalues of n does the first algorithm beat the second algorithm?

Solution 5:The first 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 fasterthan an algorithm whose running time is 2n?

Solution 6:

n = 15

Page 3: Algorithms chapter 1 2 and 3 solutions

Problem 7 (1.2-3):Assume that a new Intel processor can execute 1015 operations per second. You have two algorithmsthat test whether a number is prime or not. The first algorithm uses 100n2 operations for a numberwith n decimal digits. The second uses 2n operations for a number with n decimal digits. Usingthe first algorithm, how many seconds would the Intel processor take to determine whether a 1000decimal digit number is prime? Using the second algorithm, how many seconds would the Intelprocessor take to determine whether a 1000 decimal digit number is prime?

Solution 7:The first algorithm would take 10−7 seconds (100 nanoseconds). The second algorithm would take10286 seconds. For comparison, the universe is about 4.33 × 1017 seconds old, so it would takeapproximately 2.5 × 10268 times the age of the universe to solve the problem using the secondalgorithm.

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 prob-lem 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 centurylg n√

n

n

n lg n

n2

n3

2n

n!

Solution 8:

1 second 1 minute 1 hour 1 day 1 month 1 year 1 century

lg n 210626×107

23.6×10928.64×1010

22.63×101223.16×1013

23.16×1015

√n 1012 3.6× 1015 1.3× 1019 7.46× 1021 6.92× 1024 9.96× 1024 9.96× 1030

n 106 6× 107 3.6× 109 8.64× 1010 2.63× 1012 3.16× 1013 3.16× 1015

n lg n 62746 2801417 1.33× 108 2.76× 109 7.29× 1010 7.98× 1011 6.87× 1013

n2 1000 7745 60000 293938 1621643 5617538 56175382n3 100 391 1532 4420 13802 31600 1466772n 19 25 31 36 41 44 51n! 9 11 12 13 15 16 17

john
Sticky Note
they did 365/12 for number of days in a month
Page 4: Algorithms chapter 1 2 and 3 solutions

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

Solution 9:

(n + a)b =b∑

k=0

(b

k

)nb−kak (The binomial formula)

= nb + bnb−1k + · · ·+ bnkb−1 + kb

= Θ(nb)

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

Solution 10:

2n+1 = 2× 2n so 2n+1 = O(2n)

22n = 2n × 2n so 22n 6= 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 + 33.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 Ω Ω Ω Θ3.14× 108 Ω Θ Ω Ω Ω Ω

8n2 lg4 n + n3 O O Θ Ω Ω O

2n O O O Θ O O

5n3 + 4n2 lg n + 3n O O O Ω Θ O

12n + lg8 n Θ O Ω Ω Ω Θ