assign5-problem3

assign5-problem3 - d Let s.length = n The first line is an...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 Name: Mihai Lucian Feraru a) Let a.length = n The only loop in the method runs a times. The inside of the loop contains a sum and a multiplication between two array accesses. the sum of (a * b) added into the sum is O(1) and is repeated n times. -> O(n) b) There is initially a variable declaration that only happens once. There is one conditional statement that can either happen once or not at all. there is one loop that checks the first five characters of a string, or the string's length if it is less than 5 characters The loop is executed limit times, or until it returns a true value best case: O(1) worst case: O(limit) c) The first line is a declaration that only iterates once. Let array1.length = n The first for loop iterates n times. Let array2.length = m The second for loop iterates at most m times The condition inside the second loop iterates once every loop O(1) n >= m Best case: O(1) if each array has only one number and it is the same. Worst case: O(n*m) if the two sets of numbers have no numbers in common
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d) Let s.length() = n The first line is an array declaration that only takes place once. The first for loop iterates s.length() times, and inside it, the array attribution only takes place once. O(n) c.length = s.length() = n the second for loop iterates n times. the third for loop which is nested into the second, occurs k times inside the third loop, there is a condition that can occur once or not at all, and inside it, an addition O(1). worst case: O(n + n*n) = O(n^2 + n) = O(n^2) Best case: if the string is only one character O(1) e) let big.length()-small.length() = n the first for loop iterates n times inside the loop there is a method call and a condition if there is a specific string inside a bigger string, the program returns the place where it begins worst case = O(n) if there is no "small" that equals "partOfBig" string. Sheet1 Page 2 best case = O(1) if the small string is the first part of the big string, and everything iterates only once....
View Full 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