

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Answer: A-W, B-V, C-X, D-Y, E-Z
Answer: V-B, W-A, X-C, Y-D, Z-E
for (X in A){ loop every value in array A, store in variable X
for (Y in A){ loop every value in array A, store in variable Y
if (Y/X =V) test Y divided by X , if the result equal V
print X,Y print out X,Y , the printed results are two integers a, b
For example:
Assume array A=[1,3,7,6],v=2;
when I loop X as 3 and loop Y as 6,
the result of Y divider by X is 2(equal to v)
so, 3,6 is the integers a,b.
assume the amount of value in array A is n๏ผrunning time๏ผ
๏ฌ inner for-loop is performed n times
๏ฌ outer for-loop is performed n times
๏ฌ Total ๐
2
๏ผ 1 ๏ผif sort A as first step, and use divide-and-conquer algorithm:
Step: explain:
Sort(A) sort array A, ascending from lift to right
for ( X in A){ loop the value of array A, store in X variable
for ( Y in the median of the rest array A) use divide-and-conquer algorithm to search Y
if (Y/X<v)
the rest array A = the value of the rest array A over than median
elseif (Y/X>v)
the rest array A= the value of the rest array A less than median
elseif(Y/X==v)
print out (X,Y) the printed results are two integers a, b
๏ผ 2 ๏ผAssume the amount of value in array A is n๏ผrunning time๏ผ
๏ฌ outer for-loop is performed n times
๏ฌ inner for-loop is performed :
assume total looping time is ๐, each times the number to search is:
๐
2
๐
4
๐
2
๐
and
๐
2
๐
= 1 ๏ผso ๐ = log ๐
๏ฌ Total ๐(๐) = ๐(๐ โ log ๐) = ๐(๐ log ๐)
๏ผ 3 ๏ผcompare with the algorithm in a):
๐(๐ log ๐) < ๐(๐
2
So, โsort A as our first stepโ is a faster algorithm and can reduce running time.