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)
(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);...
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)
42. Describe the order of growth of each of the following functions using O notation. a....
42. Describe the order of growth of each of the following functions using O notation. a. N2+3N b. 3N2+N c. N5+100N3+245 d. 3NlogN+N2 2 e. 1+N+N2 +N3 +N4 f. (N * (N − 1)) / 2 Describe the order of growth of each of the following code sections, using O notation: count = 0; for (i = 1; i <= N; i++) count++; count=0; for (i = 1; i <= N; i++) for (j = 1; j <= N; j++)...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O: B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
I have type O blood. My dad has type O blood. My mother has type A...
I have type O blood. My dad has type O blood. My mother has type A blood. How is this possible? What is my, my dad's, and my mother's genotype for blood type? What are the possible blood types of my siblings? Is blood type and example of "incomplete dominance" or, "codominance"?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT