The aim of Questions 2-6 is to analyze the time complexities of three divide and
ID: 3592080 • Letter: T
Question
The aim of Questions 2-6 is to analyze the time complexities of three divide and conquer methods, beginMax, middleMax, and endMax, for finding a cell with a maximum key value on a linked st. The list can be split at the beginning (the first sublist has size 1 and the second one has size 1), or in the middle (the first sublist has sizeand 17 the second sublist has size or it can be split at the end (the first sublist has size n and the second sublist has size 1). The three variants have similar programs. The code given on the last page other two variants, line B1 which says last = n/2; is replaced with last = 1; (if the split is at the beginning), or last-n-l; (if the split is at the end). Also, the conquer steps for beginMax and endMax in lines Cl and C2 are recursive calls to beginMax and endMax respectively of this assignment does the split in the middle. To get the As suggested in the text (pp. 182-184), one approach to estimating algorithm time complexities is to identify an operation (or operations) such that the order of growth of the running time of an algorithm is the same as the order of growth of the function repre- senting the number of times the operation occurs on a given problem size. To simplify the analysis, instead of choosing a proxy operation, I want you to count the exact number of times that one of the designated statements A4, B2, B4, C1, C2, or Di is executed. This properly accounts for the time complexities of the base case (it takes O(1) time and we count executions of A4), the divide step of the algorithm (the time it takes is proportional to the work done by the loop for which we count executions of B2, plus a constant number of other steps for which we count executions of B4), and the marriage (the work done is in O(1) and we count executions of D1). The recursive calls themselves have O(1) overhead and this is accounted for by counting executions of C1 and C2 For a list of size, B(n) will be used to denote the number of times a designated statement is executed for beginMax, M(n) will be used to denote number of times a des- ignated statement is executed for middleMax and E(n) will be used to denote number of times a designated statement is executed for endMax.Explanation / Answer
public class InteractiveAverageMethods_AdaptedForAsgn05 {
// Note that the Scanner an Toolkit objects are preceeded by 'static' and
// appear before the main program. This gives them a global scope.
static Scanner console = new Scanner(System.in);
static Toolkit tools = new Toolkit();
static final int NUMBER_WIDTH = 0; // # of spaces to display numbers
public static void main (String [] args) {
double num1 = 0.0; // First input value
double num2 = 0.0; // Second input value
double grossPay = 0.0;
double savingsRate = 0.0;
double iraRate = 0.0;
double average = 0.0; // Average of the input values
// Explain the program to the user
explain();
// Input the numbers
grossPay = getOneNumber("gross pay");
savingsRate = getOneNumber("savings rate");
iraRate = getOneNumber("IRA rate");
// Determine the average of the two numbers
average = calcAvg(num1, num2);
// Output the results
outputResults(num1, num2, average);
System.exit(0);
} // End main
// **************************************************************
// Methods section
// Explain the program to the user
public static void explain() {
System.out.println(
"This program averages two real numbers " +
"entered by the user. " +
"The output is the two numbers and their average. " +
"Note: methods are used.");
} // End explain
// ***************************************************************
// Return the number input by the user
public static double getOneNumber(String phrase) {
double num; // Number input by the user
System.out.print("Enter your " + phrase + ": ");
num = console.nextDouble();
return num;
} // end getOneNumber
// ***************************************************************
// Return the average of two numbers, a and b
public static double calcAvg(double a, double b) {
return (a + b) / 2.0;
} // End calcAvg
// ***************************************************************
// Output the values of numbers first and second and their average
public static void outputResults(double first,
double second,
double average) {
System.out.print(
"The average of " +
tools.leftPad(first, NUMBER_WIDTH, "0.0") +
" and " + tools.leftPad(second, NUMBER_WIDTH, "0.0") +
" is " + tools.leftPad(average, NUMBER_WIDTH, "0.00"));
} // End outputResults
} // End class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.