Академический Документы
Профессиональный Документы
Культура Документы
1. Describe an algorithm that finds the 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?
Finally the 5th largest element and the smallest element are fixed.
Sorted list – 1 2 3 4 5 6
Solution:
a) (n2+8) (n+1)
n3+n2+8n+8
Then we have
.·.Big-O is O(n5)
Solution:
a) 88
88/2 = 44
44/2 = 22
22/2 = 11
11/11 = 1
b) 126
126/2 = 63
63/3 = 21
21/3 = 7
7/7 = 1
729/3 = 243
243/3 = 81
81/3 = 27
27/3 = 9
9/3 = 3
3/3 = 1
d) 1001
1001/7 = 143
143/11 = 13
13/13 = 1
Solution:
a) f(x) = 3x+7
f(x) = 3x + 7
Then we have
f(x) = x2+x+1
Then we have
c) f(x) = 5logx
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.