Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

AKSHAI D PILLAI

AM.EN.U4AIE22102

DESIGN AND ANALYSIS OF ALGORITHMS


LABSHEET 1
AKSHAI D PILLAI

AM.EN.U4AIE22102

1.

a. The loop executes (N-1) mes


b. No, the number of mes the loop executes does not change as the elements in the array
change. The minimum and maximum number of mes the loop executes are both N-1,
where N is the size of the array.
c. The me complexity of the program is O(N), where N is the size of the array.

2.

a. No, we don't need any loops in this program because we can directly access the first and
last elements of the sorted array to find the smallest and largest elements, respec vely.
b. b. The me complexity of the program is O(1), which means it runs in constant me.

3.
AKSHAI D PILLAI
AM.EN.U4AIE22102

a. As the elements in the array change (but the size remains the same), the number of
mes the loop executes may change
b. The me complexity of the program is O(N), where N is the size of the array.

4.

a. No, the program for ques on 3 won't work efficiently for searching in a sorted array
because it doesn't take advantage of the fact that the array is sorted.
b. No, the program for ques on 3 is not the most efficient one for searching in a sorted
array.

c.
d. As the elements in the array change (but the size remains the same), the number of
mes the loop executes may change.
e. The me complexity of the binary search algorithm is O(log N), where N is the size of the
array.
AKSHAI D PILLAI
AM.EN.U4AIE22102

5.

a. The me complexity of this program is O(N), where N is the size of the array.

6.

a. The me complexity of this algorithm is O(sqrt(n))


AKSHAI D PILLAI
AM.EN.U4AIE22102

7.

a. The me complexity of Euclid's algorithm for finding the GCD is O(log(min(a, b))).
b. To find an input that requires the maximum number of itera ons to solve, we need to
find two numbers 'a' and 'b' such that their GCD is 1 (indica ng they are coprime) and 'a'
is significantly larger than 'b'.

You might also like