Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 4 Each of the four Java functions given here returns a string of length n whose characters are all y. Determine the order of

Question 4 Each of the four Java functions given here returns a string of length n whose characters are all y. Determine the order of growth of the running time of each function. Recall that concatenating two strings in Java takes time proportional to the length of the resulting string.

Function 1. public static String method1(int n) { char[] temp = new char[n]; for (int i = 0; i < n; i++) temp[i] = 'y'; return new String(temp); }

Function 2. public static String method2(int n) { String s = ""; for (int i = 0; i < n; i++) s = s + "y"; return s; }

Function 3. public static String method3(int n) { if (n == 0) return ""; String temp = method1(n / 2); if (n % 2 == 0) return temp + temp; else return temp + temp + "y"; }

Function 4. public static String method4(int n) { if (n == 0) return ""; if (n == 1) return "y"; return method3(n/2) + method3(n - n/2); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago