Question

2.After analyzing a few algorithms we found the primitive operations done in the algorithms as the...

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);
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

  1. In this expression n2 is the term that affects the complexity function the most and hence the complexity of this algorithm is O(n2).
  2. In this expression n4 is the term that affects the graph of the complexity function the most and hence the complexity of this algorithm os O(n4).
  3. In this expression, logn is the most significant term and hence the complexity of the algorithm is O(logn).
  4. In this expression, n2 is the most significant term and hence the complexity of the algorithm is O(n2).
  5. In this expression, nlogn is the most significant term and hence the complexity of the algorithm is O(nlogn).

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!!

Add a comment
Know the answer?
Add Answer to:
2.After analyzing a few algorithms we found the primitive operations done in the algorithms as the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT