Assignment Ada 1
Assignment Ada 1
f(n)≤max(f(n),g(n))
g(n)≤max(f(n),g(n))
(f(n)+g(n))/2≤max(f(n),g(n))
max(f(n),g(n))≤(f(n)+g(n))
0≤f(n)+g(n)/2≤max(f(n),g(n))≤f(n)+g(n)
Question no 2
Answer 2
include<iostream>
#include<limits>
using namespace std;
int maxArray(int a[], int n
{
int max = 0, max_current = 0,st =0, ed = 0, s=0;
for (int i=0; i< n; i++ )
{
max_current += a[i]
if (max < max_current)
{
max = max_current;
}
if (max_current < 0)
{
max_current = 0;
s = i+1;
}
}
for(int i=st;i<=ed;i++)
cout << a[i] <<" ";
return maxi;
}
int main()
{
int n,maximum;
cin >> n;
int a[n];
cout << "Enter the Array : ";
for(int i=0;i<n;i++)
cin >> a[i];
maxm = maxArray(a, n);
return 0;
}
Question no 3
Answer 3
Let us assume n=2mn=2m, m=lognm=lgn.
Hence, the recurrence takes the form
T(2m)=3T(2m/2)+mT(2m)=3T(2m/2)+m.
Now, assuming S(m)=T(2m)S(m)=T(2m), the recurrence takes the form
S(m)=3S(m/2)+mS(m)=3S(m/2)+m.
Let’s assume, S(m)≤cmlg3S(m)≤cmlg3, for all m≥m0m≥m0, where cc and m0m0 are some
positive constants.
S(m)=3S(m/2)+m≤3c(m/2)lg3+m=3cmlg32lg3+m=cmlg3+mS(m)
=3S(m/2)+m≤3c(m/2)lg3+m=3c2lg3mlg3+m=cmlg3+m
So, let us modify our assumption by subtracting a lower-order term.
Question no 5
Answer no 5
a) First, analyze the asymptotic behavior of the given function f(n)=10n2+7n+3f(n) =
sqrt{10n^2 + 7n + 3}f(n)=10n2+7n+3:
2nlog(n+2)2=4nlog(n+2)
4nlogn
Question no 6
Answer no 6
we check elements one by one, and if the element is equally likely to be anywhere in the array
of size n, on average, we will find it halfway through the array. Therefore, the number of
elements checked on average is:
element is not in the array or is the last element of the array. Therefore, we need to check all n
elements. Thus, the number of checks in the worst case is:
Question no 7
Answer no 7
Yes, this can be achieved by augmenting a sorting algorithm like bubble sort or
insertion sort.
Bubble sort can be augmented by adding a flag to detect if any swaps were made
during a pass. If no swaps are made, the array is already sorted, and we can
terminate early. In this case, the number of comparisons in the best case is n−1n-
1n−1 because we only compare each adjacent pair once in a sorted list.
Insertion sort already has n−1n-1n−1 comparisons in the best case when the list is
sorted. In each iteration, it compares the current element with the last sorted
element, and if the list is already sorted, no further comparisons are made.
Thus, augmenting bubble sort or using insertion sort can lead to a best-case
comparison count of n−1n-1n−1. This would indeed be a worthwhile addition to a
sorting algorithm in scenarios where the input is already nearly or completely
sorted, as it would significantly improve performance in those cases.
Question no 8
Answer no 8
Peasant takes Goat across the river.
Wolf is on the other side, but they don’t interact with the cabbage.
Peasant returns alone and finally takes the Goat across the river.
Question no 9
Answer 9
Pseudo code for min heapify
MIN-HEAPIFY(A, i)
left = LEFT(i)
right = RIGHT(i)
smallest = i
if smallest ≠ i then
swap A[i] with A[smallest]
MIN-HEAPIFY(A, smallest)
The running time of MIN-HEAPIFY is the same as that of MAX-HEAPIFY. Both take O(log n) time,
where n is the number of elements in the heap. This is because in both cases, you are comparing the
current element with its children and potentially recursing down a single path of the tree, which has a
height of log n.
Question 10
Answer 10
Insertion Sort works well with nearly sorted input because it has a best-case time
complexity of O(n) when the input is already sorted or nearly sorted. The algorithm only
shifts elements that are out of order, which minimizes operations.
Quicksort, on the other hand, has an average time complexity of O(n log n) and can
degrade to O(n^2) in the worst case (if poor pivots are chosen, such as always choosing
the smallest or largest element in a nearly sorted array).
Insertion Sort will have to perform minimal operations as most elements are already in place.
Quicksort may have to make many unnecessary comparisons and recursive calls, especially if
the pivot selection leads to unbalanced partitions.