Question
*JAVA ONLY* Defend all answers based on specific references to the code. Do not count return statements or initialization of method arguments. You are strongly
*JAVA ONLY*
Defend all answers based on specific references to the code. Do not count return statements or initialization of method arguments. You are strongly encouraged to walk through algorithms in the debugger and to add statement-counting code to given methods to test and refine your analysis. All growth functions must be in simplified t(n) = ____ format and order must be presented in proper big-O notation.
/** * Replace all occurrences of oldValue with newValue in array. * @param array ints where oldValue may be found * @param oldValue value to replace * @param newValue new value */ public static void replaceAll(int[] array, int oldValue, int newValue) { int index = find(array, oldValue); while (index > -1) { array[index] = newValue; index = find(array, oldValue); } }
Algorithm: replaceAll()
1.
A) Minimum Statements. How many statements would be executed in a call to replaceAll() when the array size is zero (n == 0)?
B) Best Case Scenario. Under what conditions would the minimum number of replaceAll() loop iterations be executed for an array where n is large? What would cause the replaceAll() loop to never iterate even if n is large? What is the cost of the find() call(s) under these conditions? What is the total growth function under these conditions?
C) Worst Case Scenario. Assuming newValue and oldValue are not equal, under what conditions would the maximum number of replaceAll() loop iterations be executed for an array where n is large? Note that every call to find() inside the replaceAll() loop is guaranteed to iterate a different number of times, so what would the average number of find() loop iterations be?What is the total growth function under these conditions?
Additionally, what arguments could I add to the command line or surrounding the method itself to verify the answers to these questions?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started