Dsa Theory As1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

QUESTION 1:

1. Initialize two variables, max and min, to the first element of the array.

2. Starting from the second element, compare each element with the current max and
min. If an element is greater than the current max, update max to that element. If an
element is less than the current min, update min to that element.

3. Repeat step 2 for all elements in the array.

The time complexity of this algorithm is O(n), as each element in the array is visited once and
compared with the current max and min.

The step count for this algorithm is 2n-2, n for comparing and updating max and min and n-2 for
the iteration

QUESTION 2:
1. Initialize a variable, found, to false.

2. Starting from the first element, compare each element with the target element. If a
match is found, set found to true and break out of the loop.

3. Repeat step 2 for all elements in the array.

4. If found is true, return the index of the target element. If found is false, return -1 to
indicate that the element is not present in the array.

The time complexity of this algorithm is O(n) as in the worst case, the element is not present in
the array and we have to visit all the elements to find it.

The step count for this algorithm is n, where n is the number of elements in the array, as we
have to visit each element once.

QUESTION 3:
1. Initialize two variables, low and high, to the first and last indices of the array.

2. While low is less than or equal to high, repeat the following steps: a. Find the middle
index of the current subarray by (low + high) / 2 b. Compare the element at the middle
index with the target element. c. If they are equal, return the middle index. d. If the
element at the middle index is less than the target element, update low to middle + 1. e.
If the element at the middle index is greater than the target element, update high to
middle - 1.

3. If the loop is exited, return -1 to indicate that the element is not present in the array.

The time complexity of this algorithm is O(log n) as the size of the subarray to be searched is
halved at every step.
The step count for this algorithm is log(n), as in each iteration the algorithm divides the array in
half and the number of iterations required to find the element is logarithmic with respect to the
size of the array.

QUESTION 4:
algorithm B starts to show its better performance when n is greater than a number that is
dependent of the logarithm base and it grows slower than sqrt(n)

QUESTION 5:
algorithm A starts to show its better performance when n is less than a number that is
dependent of the logarithm base and it grows slower than n^2.

QUESTION 6:
We can compare the performance of the two algorithms by finding the value of n at which the
number of basic operations performed by algorithm A becomes less than that performed by
algorithm B.

We know that the number of basic operations performed by algorithm A is 2000000000, and the
number of basic operations performed by algorithm B is 6n.

So, if we set the equation 2000000000 < 6n, we can solve for n to find the point at which
algorithm A starts to perform better than algorithm B.

2000000000 < 6n

n > 2000000000/6

n > 3333333.333

Algorithm A starts to show its better performance when n > 3333333.333, for values of n greater
than this, algorithm A will be more efficient

You might also like