assign5-problem3

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background 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

This note was uploaded on 11/03/2009 for the course CS 01-111 taught by Professor Pradiphari during the Fall '09 term at Rutgers.

Page1 / 2

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online