Вы находитесь на странице: 1из 5

ASSIGNMENT 6

1. Describe an algorithm that finds the sum of all the integers in a list?

Solution: Input : Integers a1, a2, a3,……,an in a list

Output : Sum of all the integers in a list

Initialize: i := 1, s:=0

while(i<=n)

s = s+ai

i = i+1

return (s)

2. Use the bubble sort to sort 6,2,3,1,5,4 showing the lists obtained at each step?

Solution: 1st Iteration: 623154

i) 6>2 → 263154 (swap)

ii) 6>3 → 236154 (swap)

iii) 6>1 → 231654 (swap)

iv) 6>5 → 231564 (swap)

v) 6>4 → 231546 (swap)

Now the largest element ie 6 is fixed.

2nd Iteration: 231546

i) 2<3 → 231546 (no swap)

ii) 3>1 → 213546 (swap)

iii) 3<5 → 213546 (no swap)

iv) 5>4 → 213456 (swap)

Now the 2nd largest element ie 5 is fixed.


3rd Iteration: 213456

i) 2>1 → 123456 (swap)

ii) 2<3 → 123456 (no swap)

iii) 3<4 → 123456 (no swap)

Now the 3rd largest element ie 4 is fixed.

4th Iteration: 123456

i) 1<2 → 123456 (no swap)

ii) 2<3 → 123456 (no swap)

Now the 4th largest element ie 3 is fixed.

5th Iteration: 123456

i) 1<2 → 123456 (no swap)

Finally the 5th largest element and the smallest element are fixed.

Sorted list – 1 2 3 4 5 6

3. Give as good a big-O estimate as possible for each of these functions:

Solution:

a) (n2+8) (n+1)

n3+n2+8n+8

For n>1, we have n<n3 and 1 <n3

Then we have

f(x) = n3+n2+8n+8 ≤ n3+n3+8n3+8n3 = 18n3

Thus f is O(n3) with witnesses C = 18 and K = 1

b) (n log n+n2) (n3+2)

n4 log n + 2n log n + n5 + 2n2

n5 + (n4+2n) log n + 2n2

To find the big-O notation for (n4+2n)logn


Big-O for (n4+2n) is O(n4)

Big-O for log n is O(log n)

.·. Big-O for (n4+2n)log n is O(n4 log n)

Big-O for (n5 + 2n2) is O(n5)

.·.Big-O for n5 + (n4+2n) log n + 2n2 is

O(max(n4 log n, n5))

n4 log n ≤ n5 for n>1

.·.Big-O is O(n5)

4. Find the prime factorization of each of these integers

Solution:

a) 88

Dividing 88 with prime numbers

88/2 = 44

44/2 = 22

22/2 = 11

11/11 = 1

Prime factorization of 88 = 2·2·2·11 = 23·11

b) 126

Dividing 126 with prime numbers

126/2 = 63

63/3 = 21

21/3 = 7

7/7 = 1

Prime factorization of 126 = 2·3·3·7 = 2·32 ·7


c) 729

Dividing 729 with prime numbers

729/3 = 243

243/3 = 81

81/3 = 27

27/3 = 9

9/3 = 3

3/3 = 1

Prime factorization of 729 = 3·3·3·3·3·3 = 36

d) 1001

Dividing 1001 with prime numbers

1001/7 = 143

143/11 = 13

13/13 = 1

Prime factorization of 1001 = 7·11·13

5. Determine each of these functions is O(x) ?

Solution:

a) f(x) = 3x+7

f(x) = 3x + 7

Then we have

f(x) = 3x+7 ≤ 3x+7x = 10x

Thus f is O(x) with witnesses C = 10 and K = 1


b) f(x) = x2+x+1

f(x) = x2+x+1

For x>1, we have x<x2 and 1<x2

Then we have

f(x) = x2+x+1≤ x2+x2+x2 = 3x2

Thus f is O(x2) with witnesses C = 3 and K = 1

c) f(x) = 5logx

Big-O notation for 5log x is O(log x)

6. Suppose that an element is known to be among the first 4 elements in a list of 32


elements. Would a linear search or binary search locate this element more rapidly?
Explain your answer?

Solution:

Linear search is better than binary search as linear search will take less time to find the
answer than binary search. In linear search the number to be found out is being compared
with each element from the beginning of the list unlike binary search where the list is first
divided into two halves and number is checked in which half it belongs and then then the
procedure is repeated till number if found.

Вам также может понравиться