2.After analyzing a few algorithms we found the primitive operations done in the algorithms as the following functions of their input sizes n. Find the big O of the algorithms and sort them from the fastest to the showest algorithms.
1. T1(n) = 5n2+ 20n + 15
2. T2(n) = 6n3+ 8n4+100
3. T3(n) = 7log(n) + 4
4. T4(n) = 2n+ 3n2 + 1
5. T5(n) = 5nlog(n) + 3log(n) + 3n
3.In an attempt to print numbers from 1 to n a student wrote the following recursive program. Carefully examine the code and determine the possible mistakes the student has made.
printNum(int n){
System.out.print(n);
printNum(n-1);
}
Main(){
printNum(5);
}
Solution 1: Given the function that describes the working of an algorithm, you can find out the complexity of this algorithm by simply finding out the term within the function that affects the graph of the function the most, so for the above-given functions, their complexity can be determined by finding out the most significant terms out of them, same is done below:
Solution 2: While designing any recursion based solution you need to make sure that there is a terminating condition for the recursion, otherwise the function would just keep on calling itself infinitely and would lead to an infinite loop and the memory leakage. The same would happen in the above-given code, as this would also go to the infinite loop. In order to ensure that the recursion is terminated, you need to plug in some terminating condition, the same is done in the corrected code down below:
Code:
printNum(int n){
if (n < = 0) //Terminating condition
return;
else
{
System.out.print(n);
printNum(n-1);
}
}
Main(){
printNum(5);
}
Here's the solution to your question, please provide it a 100% rating. Thanks for asking and happy learning!!
2.After analyzing a few algorithms we found the primitive operations done in the algorithms as the...
This is the sequence 1,3,6,10,15 the pattern is addin 1 more than last time but what is the name for this patternThese are called the triangular numbers The sequence is 1 3=1+2 6=1+2+3 10=1+2+3+4 15=1+2+3+4+5 You can also observe this pattern x _________ x xx __________ x xx xxx __________ x xx xxx xxxx to see why they're called triangular numbers. I think the Pythagoreans (around 700 B.C.E.) were the ones who gave them this name. I do know the...