C prove that c n is θ d n answer since we have just

Info icon This preview shows pages 3–5. Sign up to view the full content.

View Full Document Right Arrow Icon
C. Prove that C ( n ) is Θ( D ( n )). Answer Since we have just shown that C ( n ) and D ( n ) are Big-Oh of each other it follows that C ( n ) is Θ( D ( n )). Problem 5: 20 points Analyze the following fragment of code and give a Big-Oh characterization of its running time (that is, give the best upper bound on the worst-case running time of the algorithm that you can find). Explain your analysis. public static void doWork (int[] arr) { for (int i=1; i < arr.length; i++) { for (int j=0; j < i; j++) { System.out.print(j); } } } Answer Let n = arr . length be the size of the input. The body of the inner loop runs in time O (1). For each i = 1 , . . . , n - 1 the inner loop executes i times therefore it runs in time O ( i · 1), that is O ( i ). But i < n so the each of the executions runs in time O ( n ). Therefore the body of the outer loop runs in time O ( n ). The outer loop executes n - 1 times so it runs in time O (( n - 1) n ), that is, O ( n 2 ). The whole method runs in time O ( n 2 ). Note. An alternative more detailed analysis computes the sum n - 1 X i =1 i = ( n - 1)( n - 1 + 1) 2 = n 2 - n 2 but still, the (asymptotically) best we can state is that the method runs in time O ( n 2 ). 3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem 6: 20 points Write a Java method that takes an argument an array a of type int[] and returns true if the elements of a are in strictly increasing order and false otherwise. Then, analyze your method and give a Big-Oh characterization of its running time (that is, give the best upper bound you can find on the worst-case running time of your method). Explain your analysis. Answer One method that accomplishes this task goes as follows: public static bool sorted(int[] arr) { if (arr.length<2) return true; for (int i=0; i<arr.length-1; i++) { if (arr[i]>=arr[i+1]) { return false; } } return true; } Let n = arr . length be the size of the input. The worst case running time is when the array is sorted in strictly increasing order. In this case the for the loop iterates n - 1 times. The body of the loop runs in time O (1). Thus, we can conclude that the method runs in time O ( n ). Extra Credit 1: 15 points In this problem you are NOT allowed to use the theorems about Big-Oh stated in the lecture notes. Your proof should follow just from the definition of Big-Oh. (As usual in problems with Big-Oh, we assume that the functions map nonnegative reals to strictly positive reals.) Let f ( n ) be such that for any even n , f ( n ) = n 2 and for any odd n , f ( n ) = n 2 . TYPO We should not have formulated the problem like this because it does not give any information about f ( n ) when n is not a natural number! And it is quite possible that on arguments that are not natural numbers f would behave asymptotically worse than n 2 ! We reformulate the problem as follows: Let f ( n
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern