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

What is the output of the following code? Explain your answer in a paragraph or two....
What is the output of the following code? Explain your answer in a paragraph or two. class ExceptionThrown { static int divideByZero(int a, int b){ int i = a/b; return i; }    static int computeDivision(int a, int b) { int res =0; try{    res = divideByZero(a,b); } catch(NumberFormatException ex){ System.out.println("NumberFormatException is occured"); }    return res; }    public static void main(String args[]){ int a = 1;    int b = 0; try{ int i = computeDivision(a,b); }...
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)...
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.
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 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...
Determine whether the following arguments are valid. Explain your answer. If it is hot outside, I...
Determine whether the following arguments are valid. Explain your answer. If it is hot outside, I will not go to play. If I play, I will not study. If I study, I will do good in the exam. I did good in the exam. Therefore, it is hot outside. If I can run faster than air plane or I can kill a lion then I am a super human. If I am a super human then I am very careful...
Given the below code, compute the values of the best case, worst case and average case...
Given the below code, compute the values of the best case, worst case and average case knowing that the basic operation in the first loop takes 4 ns to execute and the basic operation in the second loop takes 5 ns to execute. int A[20]; for (int i=0; i < 20; i++) {           cin >> A[i]; } for (i=0; i < 20; i++) {           if (A[i] % 3 == 0)                     break; }
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.”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT