Question

In: Computer Science

Question1 if possible, could anyone do with explanation For each of the following code snippets, give...

Question1

if possible, could anyone do with explanation
For each of the following code snippets, give both of the following:
a. Give the overall T(n) run me analysis expression for the code.
b. Describe the worst case running me of the code snippet in Big‑Oh notation.

1.for (int i=0;i<n;i++)

{

for (int j=0;j<n/2;j++)

{ sum++;}

}

2.void silly(int n,int x,int y)

{ for (int i=0;i<n/2;i++)

{ if (x<y)

{ for (int j=0;j<100*n*n;j++)

{ cout<< "j="<<j<<endl;}}

else {cout <<"i="<<i<<endl;}}}

3.void silly(int n,int m)

{ if (n<1)

{ return ;}

if (n<m)

{ silly(n/2,m+1); }

else

{ silly (n/2, m); }}

4.for (int i=0;i<3*n;i++)

{

sum++;

}

for (int i=1;i<ni*=2)

{ for (int j=0;j<n*n*n;j++)

{ sum++; }}}

5. for (int i=0;i<n;i++)

{

for (int j=0;j<std::min(10,i);j++)

{ sum++;}}

Question 2
Given the following list of values, perform a binary search for the indicated search item. Show the values of first, last and middle, and the number of comparisons made after each iteration of the loop. Your should create a table like the following to show your work, where first and last are the values of the variables at the beginning of the loop, and mid is the midpoint used during that iteration, list[mid] is the value in the list at the midpoint, and No. of key comparisons should be the number of comparisons made during this iteration by the algorithm:

Iteration first last mid list[mid] no of key comparisons

1 0 12 ? ? ?

list [7,9,9,9,10,12,19,20,22,28,28,29,29]

Searching for value:22

Solutions

Expert Solution

Question 1.

1. Refer to the below image for how to calculate the time complexity for the 1st part.

  

The first for loop runs for n times, while for every iteration of the first for loop, the second for loop runs (n/2) times. Thus, the total number of times that the sum++; statement runs is n*(n/2) which is O(n2).

Hence, T(n) =  O(n2).

2. Refer to the below image for how to calculate the time complexity for the 2nd part.

The first for loop runs for (n/2) times, while for every iteration of the first for loop, the second for loop runs (100*n*n) times.

Hence, T(n) = (n/2)*(100*n*n) = O(n3).

3. Refer to the below image for how to calculate the time complexity for the 3rd part.

When the value of n is 1, the first if statement will become true and hence the time complexity will be O(1). but in the worst case, the 2nd or 3rd if statement will be executed and thus, the time complexity will be O(log2n), since n is decreasing exponentially, by powers of 2. It is clearly explained in the above figure.

4. The second for loop, i.e., for (int i=1;i<ni*=2), is not clear in this part. There is no variable called ni, and you can't assign a value to a variable inside a for loop statement. Thus, this statement is incorrect.

5. This part is a little tricky. Read carefully to understand better.

The std::min(a,b) function returns the minimum of the two values a and b. For example, std::min(8, 3) will return 3.

This means that std::min(10, i) will return whichever is minimum between 10 and i.

Thus, when i<10, std::min(10, i) will always return i. When i>=10, std::min(10, i) will return 10.

This means that for i=1, the nested "for" loop will run for 1 time, for i=2, it will run for 2 times, for i=3, it will run for 3 times, and so on till i=10. Thus, for i = 0 to 9, the nested loop will run for (1+2+3+...+9) times.

When i>=10, the nested loop will always run for 10 times, independent of the value of i. Thus, for i=10 to (n-1), the nested for loop will run for ((n-10)*10) times.

Thus, the total time complexity will be:

T(n) = (1+2+3+...+9) + ((n-10)*10)

= 45 + 10*n - 100

= O(n)

Hope, you have understood how to calculate the time complexities for the given code snippets. If it helped, please consider giving positive ratings :)


Related Solutions

For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
This is a numerical methods question using MATLAB. Which of the following code snippets finds the...
This is a numerical methods question using MATLAB. Which of the following code snippets finds the forward difference estimate of the derivative at each of the x values. Assume x and y have been previously defined, for example as y=[10,20,25, 27.5, 30]; x = [0.3,0.5, 0.8, 0.9, 1]; (d is the derivative variable name) Although not necessarily so, there may be more than one correct answer. a) for k=1:length(y)-1 d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end d(k+1)=NaN b) for k=1:length(y) d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end c) d(1)=NaN; for...
Draw and give examples where possible to support your answers. QUESTION1. Suppose the market for coffee...
Draw and give examples where possible to support your answers. QUESTION1. Suppose the market for coffee is currently in equilibrium at a price of RM 3.00. An early frost coffee-growing nations decreases the supply of coffee. Use supply and demand analysis to forecast the impact of the freeze on the market equilibrium price and quantity of coffee . Please the diagram is very important I need it ! Please draw it !
Can anyone give a detailed explanation with some examples on these questions? I am going to...
Can anyone give a detailed explanation with some examples on these questions? I am going to have an exam soon. 1. How should we account for the borrowing costs that we incurred this year on constructing the Torry warehouse facility? 2. Why would a machine, that we have started leasing on a 10 year agreement, have to be shown on our statement of financial position when we don’t own it? 3. The Board of ABC Corp., a manufacturer of electrical...
could anyone make a php code for a cart page for an e commerce website?? just...
could anyone make a php code for a cart page for an e commerce website?? just the cart page not the whole website
Give a brief explanation of possible testing process for each system below: 1. Lunar greenhouse 2....
Give a brief explanation of possible testing process for each system below: 1. Lunar greenhouse 2. Nuclear fission reactor on the moon 3. Lunar rover 4. Spacesuits
Sorry there is no more information i could provide.Please give each step's code in two class....
Sorry there is no more information i could provide.Please give each step's code in two class. This question is about the java program and it conclude six step.Please show every step's code in two class and answer the question :"How many tests should the testStudent method now contain" "Do not forget to modify your testStudent method to test the new code. "and please explain why?Thank you! Step1: Create a class Student that contains the following things:  a private integer...
Give a briefly explanation of each of the following five main causes of pollution :
  Give a briefly explanation of each of the following five main causes of pollution : - Exhaust and emissions from vehicles and industries. -Dumping waste in water bodies. - Accumulation of non-biodegradable waste (like plastic). - Excessive use of chemicals (herbicides and pesticides) in agriculture. - Deforestation and overgrazing.  
Give a briefly explanation of each of the following Solution to pollution problem: - Afforestation. -...
Give a briefly explanation of each of the following Solution to pollution problem: - Afforestation. - Use of biodegradable material instead of plastic. - Use of mass transportation. - Treatment of chemicals before release from industries. - Switching to renewable sources of energy like solar energy.
Give a possible set of quantum numbers for an electron in each of the following and...
Give a possible set of quantum numbers for an electron in each of the following and explain what each number stands for. a) a 3s orbital b) a 2p orbital c) a 5d orbital
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT