Question

In: Computer Science

I have a function and I was tild to give the big o notation of it...

I have a function and I was tild to give the big o notation of it
Analyze the worst-case run-time complexity of the member function reverse() of the List. Give the complexity in the form of Big-O. Your analysis can be informal; however, it must be clearly understandable

template <typename T>

void List<T>::reverse() {

auto current = head; // starting from head node

  while(current != nullptr) // till current node is not last (next after last)

  {

    std::swap(current->next, current->prev); // swap 'next' and 'prev' pointer values

    current = current->prev; // advance next node (we use 'prev' because we swap it before)

  }

  std::swap(head, tail); // swap 'head' and 'tail' nodes

}

Solutions

Expert Solution

Well The worst time complexity for reversing the list would be O(n).

The possible explaination i can give is :

Time complexity refers to how much time would the program take to complete.

Now suppose there is a list with 1 element how many operation would it take to reverse it .It would be one.

Now suppose there are 2 elements in list you have to reverse 2 elements so the time complexity will be dependent on reversing those two elements.

Now in worst case if there are n elements in list how much operation would it take to reverse n elements

The answer is n.So time complexity is O(n) because we need to iterate through whole list i.e process n elements.

Next example will make it more clear:

Suppose there is a nested loop such as

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

{

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

{}

}

How will you measure the time complexity of such a program

Now lets take i = 1 how long will i stay 1 it would be untill j goes from 1 to n.

after that i would become 2.

similarly when i =2 when will i become 3.

when j goes from 1 to n.

SO what can we conclude

for every i we have n operation in the inside loop

so in worst case if i = n it will take n*n time i.e O(n^2)

So we can say that in above given code while reversing we have to do n operations (n = no of elements in list)

So time complexity would be O(n).


Related Solutions

1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f(x, y) is O(g(x, y)) means that there exist constants C, k1, and k2 such that|f(x, y)|≤C|g(x, y)|whenever x > k1 and y > k2] 2. Find a div b and a mod b when: (a) a = 30303, b = 333 (b) a = −765432, b = 3827 3. Convert...
How to determine whether the following statements about big-O notation are true or false? (a) Let...
How to determine whether the following statements about big-O notation are true or false? (a) Let f(n) = √ n log n − 4, then f(n) = O(n^ 2) (b) Let f(n) = 4 n + 2 log^ 2 (n), then f(n) = O(log^ 2 (n)) (c) Let f(n) = 5 √ n + 2, then f(n) = Ω(log^ 4 (n)) (d) Let f(n) = 5 n^ 2 + 5 n log n + 4, then f(n) = O(n^3 )...
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2....
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2. 100nlog n + n5 + 100n 3. n2log n + nlog n 4. 2n + n5 + 5n 5. 100n + 0.01n2 A. O(n3) B. O(5n) C. O(n2) D. O(n5) E. O(n2log n)
Please explain what the Big O Notation is in plain English. Please include the logic behind...
Please explain what the Big O Notation is in plain English. Please include the logic behind it and calculations if necessary. I have trouble understanding it, I will be taking a test soon so I will need an easier way to remember it. I use Python btw.
(a) Explain the difference between big-O and big-theta, and give examples of each to show the...
(a) Explain the difference between big-O and big-theta, and give examples of each to show the difference. (b) How can we say that two functions have the same asymptotic complexity, using big-theta notation? (c) Rank the following functions in order of increasing complexity (rate of growth), and indicate which functions have the same asymptotic complexity. x2; x log(x); 2x; log(x) + 7; 92x2 + 57x + 3921; 4x; 27x2 + 8x3; 22x+5; log(x42); 3x + 12.
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
* Please I want answer for these questions and I will give big thump for it....
* Please I want answer for these questions and I will give big thump for it. Thanks Read the following case study and answer the following question. The Five star Hotel ELV (Extra Low Voltage) project was located in Bahrain and completed in 2011. The purpose of this project was to install, test, and commission the IT, communication infrastructure, and services for the hotel. The ELV project was part of a total program to deliver 11 sub system, including installation...
Please I want answer for this and I will give big thump. Thanks Generally in project...
Please I want answer for this and I will give big thump. Thanks Generally in project management, there are three basic forms of organizational structure that is functional, project and matrix. Suppose you are responsible for advising an health care product company on changes to an organizational structure, to embrace the growing need for effective project management in creating new health care products. Draw the sample organization structure of health care products and conduct an assessment of the advantages and...
Consider functions f : {1,2,3,4}→{1,2,3,4,5,6}. a) Give an example of one such function (use 2-line notation)....
Consider functions f : {1,2,3,4}→{1,2,3,4,5,6}. a) Give an example of one such function (use 2-line notation). Then say how many such functions there are and why your answer makes sense.(25 points) (b)Give one example of such a function that is injective and one that is not. Then say how many injective functions there are and why your answer makes sense.(50 points) (c) Explain why there are no surjective functions with this domain and codomain. (25 points)
The following functions have been calculated as the runtime complexity of various algorithms. Identify the Big-O...
The following functions have been calculated as the runtime complexity of various algorithms. Identify the Big-O complexity, and provide witness values to support your answer. Clearly highlight your answer and show any working out you do. i. f(n) = 13 + 3n2 – 9n ii. f(n) = 3n.log2n + 7n iii. f(n) = nn + 2n5 – 7 iv. f(n) = 2log2n + 4n v. f(n) = 20 + 2n4 – n2 +n vi. f(n) = 7n3/4 +3n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT