Question

In: Computer Science

Determine Theta for the following code fragments in the average case. Explain your answer. a =...

Determine Theta for the following code fragments in the average case. Explain your answer.

  1. a = b + c;

d = a + e;

  1. sum = 0;

for (i=0; i<3; i++)

for (j=0; j

sum++;

3. sum = 0;

for (i=1; i<=n; i*=2)

for (j=1; j<=n; j++)

sum++;

4. Assume that array A contains n values, Random takes constant time,

and sort takes n log n steps.

for (i=0; i

for (j=0; j

A[j] = Random(n);

sort(A, n);

5. Assume array A contains a random permutation of the values from 0 to n - 1.

sum = 0;

for (i=0; i

for (j=0; A[j]!=i; j++)

sum++;

Solutions

Expert Solution

  1. sum = 0;

for (i=0; i<3; i++)

for (j=0; j<n;j++)

sum++;

The first loop runs for 3 times. and the secondd one for n times after which 1 operation is done so total operations =3n and

3.

sum = 0;

for (i=1; i<=n; i*=2)

for (j=1; j<=n; j++)

sum++;

In first loop i starts from 1 and doubles itself in every iteraion so it runs for log(n) many times.

and for each iteration of i the inner loop runs for n time inside which 1 operation is done so total operations is nlog(n) and

4.

for (i=0; i<n;i++)

for (j=0; j<n:i++)

A[j] = Random(n);

sort(A, n);

sort(A, n); The first 3 line takes n^2 much time since i runs for n iteration and j runs for n iterations and constant operation(Random) is done. After that sort takes nlogn so

5

sum = 0;

for (i=0; i<n;i++)

for (j=0; A[j]!=i; j++)

sum++;

In best case scenarion when A=[0,1,2,....n-1]

when i=0 second loop runs for 1 time

when i=1 second loop runs for 2 time

when i=2 second loop runs for 3 time

...

so total operations=

in every other case the number of operations exceeds the best case


Related Solutions

Understand the code and explain the code and answer the questions. Type your answers as comments....
Understand the code and explain the code and answer the questions. Type your answers as comments. #include #include using namespace std; // what is Color_Size and why it is at the end? enum Color {        Red, Yellow, Green, Color_Size }; // what is Node *next and why it is there? struct Node {        Color color;        Node *next; }; // explain the code below void addNode(Node* &first, Node* &last, const Color &c) {        if (first == NULL)...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the code is instantiated in a process and it has JUST returned from its own call to fork() and there is now a clone of that process that is itself “just about” to pick up execution. Where will the clone child pick up its execution? ii)How does the clone child know it’s a clone? How does the parent process know it is not a clone,...
Examine each of the following code fragments to identify the type of polymorphism it represents (overloading,...
Examine each of the following code fragments to identify the type of polymorphism it represents (overloading, coercion, parametric, or subtype) double x, y;    x = 5;    y = 4;      print("result= " + x / y);   b. int sum(int a, int b) {    std::cout << "Sum of ints\n";      return a + b;      }      double sum(double a, double b) {     std::cout << "Sum of doubles\n";     return a + b;      }      int main() {  ...
Determine if the following random variables are discrete or continuous. Explain your answer thoroughly. (a) The...
Determine if the following random variables are discrete or continuous. Explain your answer thoroughly. (a) The number of blades of grass on a football field. (b) The mass, in pounds, of a football player on the field.
Determine whether each of the following is an internal control strength or weakness. Explain your answer....
Determine whether each of the following is an internal control strength or weakness. Explain your answer. 1. Receiving department manager places an order with the vendor for merchandise inventory. 2. A voucher is used to certify a transaction and authorize recording of the liability. 3. Goods are received by the receiving department and documented with a receiving report. 4. Accounting department pays the vendor's invoice before goods are received. 5. Invoices are paid after completion of the invoice approval documenting...
Answer the following, citing relevant legislation and case law in your answer:
Answer the following, citing relevant legislation and case law in your answer: a) What is the parol evidence rule, and what is the court’s reasoning in applying the rule? (Maximum 100 words) b) List and explain the exceptions to the parol evidence rule.  
Determine whether the following statements are TRUE or FALSE. Briefly explain your answer. Q1) “The rate...
Determine whether the following statements are TRUE or FALSE. Briefly explain your answer. Q1) “The rate of returns of a bond is always the same as the yield to maturity of the bond.” Q2) “The nominal interest rate is always higher than the real interest rate.” Q3) “If the actual inflation rate is unexpectedly high, the realized real interest rate can be negative.”
Determine the ground electronic state of the BeC molecule. Explain your answer. The answer requires the...
Determine the ground electronic state of the BeC molecule. Explain your answer. The answer requires the molecular orbital diagram as well as an electron configuration.
Answer the following, citing relevant legislation and case law in your answer: a) What is the...
Answer the following, citing relevant legislation and case law in your answer: a) What is the parol evidence rule, and what is the court’s reasoning in applying the rule? (Maximum 100 words) b) List and explain the exceptions to the parol evidence rule. (Maximum 350 words)
Answer the following, citing relevant legislation and case law in your answer: a) What is the...
Answer the following, citing relevant legislation and case law in your answer: a) What is the parol evidence rule, and what is the court’s reasoning in applying the rule? (Maximum 100 words) b) List and explain the exceptions to the parol evidence rule. (Maximum 350 words) REFERENCE PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT