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

DSA assignment

Q1
a) Merge sort is the most apt option for this situation. Because the Merge sort has a time
complexity of O(n log n). But selection sort and insertion have a time complexity of o(n2).
We can that we’re given that DS1.get_at_index(j) takes constant time, and
DS1.set_at_index(j, x) takes Θ(k log k) time. Since random access won’t be efficient in
this case, merge sort can be utilized as it operates on the principle of divide and
conquer.

b) merge sort works best for this scenario. Merge sort has a time complexity of O(nlogn),
which is best for sorting. Using merge sort, the order of equal elements will remain due to its
stable sorting. However, this sorting algorithm requires additional spaceif the array size is too
large with limited memory space.
Based on these advantages and disadvantages merge sort due to its optimal time complexity.
However, selection sort can also be used if memory is to be considered.

c) insertion sort is the most apt option for this situation because of its efficiency on a nearly
sorted array regardless of having an average and worst-case time complexity of O(n2). Insertion
sort will be done efficiently after the array is almost sorted after the log n swaps. Other sorting
algorithms like merge and selection sort can also be utilized here. Still, insertion sort’s efficiency
in working with nearly sorted arrays gives it an advantage compared to other sorting algorithms.

Q4. This program shows a battle between teams, 'R' and 'D', where team 'R' attacks team 'D'. Each
team member has a position and health. When adjacent 'R' and 'D' members clash, the weaker loses
one health point. The output of the code is to show the health of each of the remaining players.

Q3. This program calculates the maximum number of sushi servings that can be made, given
ingredients and prices. Then it iteratively adjusts the resource availability based on the costs to
optimize the output.
Q2. The provided code consists of two parts: first, it implements a basic queue using an
array-based approach. In contrast, the second implements a stack with functionality to find the
next greater element. Both structures are demonstrated with sample inputs.

You might also like