Uploaded by Quang Huy Đặng

Homeworks W3

advertisement
3.1
1.
Step 1: Set max = 1;
Step 2: Compare max to 8, max < 8 => set max to 8
Step 3: Compare max to 12, max < 12 => set max to 12
Step 4: Compare max to 9, max > 9 => max = 12.
Step 5. Compare max to 11, max > 11 => max = 12.
Step 6: Compare max to 2, max > 2 => max = 12.
Step 7: Compare max to 14, max < 14 => max = 14.
Step 9: Compare max to 5, max > 5 => max = 14.
Step 10: Compare max to 10, max > 10 => max =14.
Step 10: Compare max to 4, max > 4 => max =14.
Return max = 14.
2.
int sum_of_integers(arr):
total = 0
for (int I = 0;i<sizeof(arr);i++)
total += i
return total
3.
a)
Compare 1 with 9 (not a match)
Compare 3 with 9 (not a match)
Compare 4 with 9 (not a match)
Compare 5 with 9 (not a match)
Compare 6 with 9 (not a match)
Compare 8 with 9 (not a match)
Compare 9 with 9 (matches)
Return i (location)
b)
Start with the array: [1,3,4,5,6,8,9,11]
Compare 5 (middle) with 9 (9 > 5)
=> Compare from 6 to 11
Compare 8 (middle) with 9 (9 > 8)
=> Compare from 9 to 11
Compare 9 (middle) with 9 (matches)
4.
1) Initialize variables low and high to 0 and n-1 respectively, where n is the length of the list a.
2) Iterate while low <= high:
a. Calculate the middle index as mid = (low + high) / 2.
b. If x is equal to the element at index mid, insert x at index mid and exit the loop.
c. If x is less than the element at index mid, set high = mid - 1.
d. If x is greater than the element at index mid, set low = mid + 1.
3) Insert x at index low in the list a. If low is greater than the length of the list, append x to the end of the
list.
5.
Step 1:
Compare 3 and 1. Since 3 is greater than 1, swap them.
Result: [1, 3, 5, 7, 4]
Step 2:
Compare 3 and 5. Since they are in the correct order, no swap is needed.
Result: [1, 3, 5, 7, 4]
Step 3:
Compare 5 and 7. Since they are in the correct order, no swap is needed.
Result: [1, 3, 5, 7, 4]
Step 4:
Compare 7 and 4. Since 7 is greater than 4, swap them.
Result: [1, 3, 5, 4, 7]
Repeat the above steps until the list is fully sorted.
Step 5:
Compare 1 and 3. Since they are in the correct order, no swap is needed.
Result: [1, 3, 5, 4, 7]
Step 6:
Compare 3 and 5. Since they are in the correct order, no swap is needed.
Result: [1, 3, 5, 4, 7]
Step 7:
Compare 5 and 4. Since 5 is greater than 4, swap them.
Result: [1, 3, 4, 5, 7]
Step 8:
Compare 5 and 7. Since they are in the correct order, no swap is needed.
Result: [1, 3, 4, 5, 7]
Repeat the above steps until the list is fully sorted.
Step 9:
Compare 1 and 3. Since they are in the correct order, no swap is needed.
Result: [1, 3, 4, 5, 7]
Step 10:
Compare 3 and 4. Since they are in the correct order, no swap is needed.
Result: [1, 3, 4, 5, 7]
Step 11:
Compare 4 and 5. Since they are in the correct order, no swap is needed.
Result: [1, 3, 4, 5, 7]
Step 12:
Compare 5 and 7. Since they are in the correct order, no swap is needed.
Result: [1, 3, 4, 5, 7]
6.
1. Initialize i = 1 (starting position) and comparisons = 0.
2. Compare x (7) with the first element, a1 (3). They are not equal, so comparisons = 1.
3. Increment i by 1 (i = 2).
4. Compare x (7) with the second element, a2 (1). They are not equal, so comparisons = 2.
5. Increment i by 1 (i = 3).
6. Compare x (7) with the third element, a3 (5). They are not equal, so comparisons = 3.
7. Increment i by 1 (i = 4).
8. Compare x (7) with the fourth element, a4 (7). They are equal, so the search is successful.
9. The location is set to i (4).
10. Return the location (4).
Therefore, it required 4 comparisons to search for x = 7 using the linear search algorithm on the given
sequence.
3.2
1.
a) f(x) = 10 is O(x)
b) f(x) = 3x+7 is O(x)
c) f(x) = x^2 + x + 1 is not O(x)
d) f(x) = 5log(x) is O(x)
2.
a) f (x) = 17x + 11 is O(π‘₯ 2 ).
b) f(x) = x2 + 1000 is O(π‘₯ 2 ).
c) f (x) = x log x is O(π‘₯ 2 ).
d) f(x) =
π‘₯4
2
is not O(π‘₯ 2 ).
e) f(x) = 2π‘₯ is not O(π‘₯ 2 ).
f) f (x) = (x3 + 2x)/(2x + 1) is not O(π‘₯ 2 ).
3.
a) n = 3
b) n = 3
c) n = 1
d) n = 4
4.
a) π‘₯ 3 is not O(g(x))
b) π‘₯ 3 is O(g(x))
c) π‘₯ 3 is O(g(x))
d) π‘₯ 3 is not O(g(x))
e) π‘₯ 3 is O(g(x))
f) π‘₯3 is O(g(x))
5. 2n! -> 3^n -> 2^n -> n^2/1000000 -> n log n -> 1000 log n -> √𝑛
6.
a) O(n^3)
b) O(n^5)
c) O(n!)
3.3
1.O(n)
2. O(n)
3. O(n)
3.4
1.
a) Yes
b) No
c) Yes
d) No
2.
a) quotient 2, remainder 5
b) quotient: -10, remainder: -1
c) quotient: 34, remainder: 7
d) quotient: 77, remainder: 0
e) quotient: 0, remainder: 0
f) Quotient:0, remainder: 3
3.
a) 10
b) 8
c) 0
d) 9
e) 6
4.
a) 1
b) -9
c) 3
d) -14
5.
a) Div: -1, mod: -12
b) Div: -99, mod: 0
c) Div: 10, mod: 309
d) Div: 123, mod: 333
6.
a) No
b) No
d) Yes
d) No
7.
a) 0
b) 0
c) 19
d) 16
8.
a) 10111
b) 101101
c) 11110001
d) 10000000001
9.
a) 27
b) 693
c) 958
d) 31775
10.
a) 101111010
b) 1110000100
c) 100010011
d) 10100001111
11.
a) 34
b) 166
c) 123
d) 101
e) 2685
12.
a) 1000101
b) 153
c) 76
13.
a mod 3 = 2
b mod 6 = 4.
a*b mod 3 = ((a mod 3)*(b mod 3)) mod 3 = 2
3.5
a) No
b) Yes
c) Yes
d) Yes
e) No
f) No
g) No
h) Yes
2.
a) 3*13.
b) 3^4
c) 101
d) 11*13
e) 17^2
f) 29*31
3.
10! = 2^8 * 3^4 * 5^2 * 7 = 3628800.
4.
1,3,5,7,11.
5.
1,7,11,13,17,19,23,29.
6.
a)φ(4)=1
b)φ(10)=3
c) φ(13)=6
7.
a) 1
b) 1
c) 7
d) 1, 41, 43, 3
e) 1
3.6
1.
x4 = 0
x2 = 1
2.
a) x2 = 7, x3 = 3
b) x2 = 0, x4 = 4
3.
a) Set S = 18, T = 19, O = 14, P = 15.
Encrypt:
f(18) = (18+10) mod 26 = 2 -> C
f(19) = (19+10) mod 26 = 3 -> D
f(14) = (14+10) mod 26 = 24 -> Y
f(15) = (15+10) mod 26 = 25 -> Z
οƒ° Encrypted of “STOP ”is “CDYZ”
b) Set L = 11, E = 4, I = 8.
Decrypt:
f^-1(11) = (11 - 10) mod 26 = 1 -> B
f^-1(4) = (4-10) mod 26 = 20 -> U
f^(8) = (8-10) mod 26 = 24 -> Y
οƒ° Decrypted of “LEI” is “BUY”
4.
a) 9
b) 73
5.
a)
y:=28
y!=0
x:=14
r:= x mod y = 14
x:=y:=28
y:=r:=14
y!=0
r:=x mod y = 0
x:=y=14
y:=r=0
end {gcd(14,28) is 14}
b)
x:=8
y:=28
y!=0
r:= x mod y = 8
x:=y:=28
y:=r:=8
y!=0
r:=x mod y = 4
x:=y=8
y:=r=4
y!=0
r:= x mod y = 0
x:=y:=4
y:=r:=0
end {gcd(8,28) is 4}
c)
x:=100
y:=101
y!=0
r:= x mod y = 100
x:=y:=101
y:=r:=100
y!=0
r:= x mod y = 1
x:=y:=100
y:=r:=1
y!=0
r:= x mod y = 0
x:=y:=1
y:=r:=0
end {gcd(100,101) is 1}
d)
x:=28
y:=35
y!=0
r:= x mod y = 28
x:=y:=35
y:=r:=28
y!=0
r:= x mod y = 7
x:=y:=28
y:=r:=7
y!=0
r:= x mod y = 0
x:=y:=7
y:=r:=0
end {gcd(28,35) is 7}
e)
x:=7
y:=28
y!=0
r:= x mod y = 7
x:=y=28
y:=r=7
y!=0
r:= x mod y = 0
x:=y:=7
y:=r:=0
end {gcd(7,28) is 7}
lcm(7,28)=(7*28)/gcd(7,28)=28
f)
x:=12
y:=28
y!=0
r:= x mod y = 12
x:=y:=28
y:=r:=12
y!=0
r:= x mod y = 4
x:=y:=12
y:=r:=4
y!=0
r:= x mod y = 0
x:=y:=4
y:=r:=0
end {gcd(12,28) is 4}
lcm(12,28)=(12*28)/gcd(12,28)=84
g)
x:=100
y:=101
y!=0
r:= x mod y = 100
x:=y:=101
y:=r:=100
y!=0
r:= x mod y = 1
x:=y:=100
y:=r:=1
y!=0
r:= x mod y = 10
x:=y:=1
y:=r:=0
end {gcd(100,101) is 1}
lcm(100,101)=(100*101)/gcd(100,101)=10100
h)
x:=28
y:=35
y!=0
r:= x mod y = 28
x:=y:=35
y:=r:=28
y!=0
r:= x mod y = 7
x:=y:=28
y:=r:=7
y!=0
r:= x mod y = 0
x:=y:=7
y:=r:=0
end {gcd(28,35) is 7}
lcm(28,35)=(28*35)/gcd(28,35)=140
Download