Analyze the time complexity and space complexity of following algorithms. 3 1 /** Returns the sum of
Fantastic news! We've Found the answer you've been seeking!
Question:
Analyze the time complexity and space complexity of following algorithms.
Transcribed Image Text:
3 1 /** Returns the sum of the integers in given array. */ 2 public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j<n; j++) total += arr[j]; return total; 4 5 6 7 8 9 /** Returns the sum of the integers with even index in given array. */ 10 public static int example2(int[] arr) { 11 12 13 14 15 } 16 17 /** Returns the sum of the prefix sums of given array. */ 18 public static int example3(int[] arr) { int n = arr.length, total = 0; for (int j=0; j<n; j++) int 19 for (int k=0; k <= j; k++) total += arr[j]; return total; } int n = arr.length, total = 0; for (int j=0; j<n; j+= 2) total += arr[j]; return total; 20 21 902 22:01 23 24 } 25 26 /** Returns the sum of the prefix sums of given array. */ 27 public static int example4(int[] arr) { 28 int n= arr.length, prefix = 0, total = 0; 29 for (int j=0; j <n; j++) { 30 prefix + = arr[j]; 31 total += prefix; 32 } return total; // loop from 0 to n-1 Sasod dos// loop from 0 to j Note ad nuo dur Sugidong s to ost al quis ono) algo heim () 20 // loop from 0 to n-1 total) count++; thoni gets Imantdia-s va // note the increment of 2 33 34 } 35 v dogo 36 /** Returns the number of times second array stores sum of prefix sums from first. */ 37 public static int example5(int[] first, int[] second) { // assume equal-length arrays 38 int n = first.length, count = 0; 39 for (int i 0; i < n; i++) { // loop from 0 to n-1 40 int total = 0; 41 for (int j=0; j <n; j++) 42 A 43 for (int k=0; k <= j; k++) total += first[k]; if (second[i] = 44 == 45 } 46 return count; 47 Scanned with CamScanner. // loop from 0 to n-1 // loop from 0 to n-1 // loop from 0 to j 101 if oding und Code Fragment 4.12: Some sample algorithms for analysis. 3 1 /** Returns the sum of the integers in given array. */ 2 public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j<n; j++) total += arr[j]; return total; 4 5 6 7 8 9 /** Returns the sum of the integers with even index in given array. */ 10 public static int example2(int[] arr) { 11 12 13 14 15 } 16 17 /** Returns the sum of the prefix sums of given array. */ 18 public static int example3(int[] arr) { int n = arr.length, total = 0; for (int j=0; j<n; j++) int 19 for (int k=0; k <= j; k++) total += arr[j]; return total; } int n = arr.length, total = 0; for (int j=0; j<n; j+= 2) total += arr[j]; return total; 20 21 902 22:01 23 24 } 25 26 /** Returns the sum of the prefix sums of given array. */ 27 public static int example4(int[] arr) { 28 int n= arr.length, prefix = 0, total = 0; 29 for (int j=0; j <n; j++) { 30 prefix + = arr[j]; 31 total += prefix; 32 } return total; // loop from 0 to n-1 Sasod dos// loop from 0 to j Note ad nuo dur Sugidong s to ost al quis ono) algo heim () 20 // loop from 0 to n-1 total) count++; thoni gets Imantdia-s va // note the increment of 2 33 34 } 35 v dogo 36 /** Returns the number of times second array stores sum of prefix sums from first. */ 37 public static int example5(int[] first, int[] second) { // assume equal-length arrays 38 int n = first.length, count = 0; 39 for (int i 0; i < n; i++) { // loop from 0 to n-1 40 int total = 0; 41 for (int j=0; j <n; j++) 42 A 43 for (int k=0; k <= j; k++) total += first[k]; if (second[i] = 44 == 45 } 46 return count; 47 Scanned with CamScanner. // loop from 0 to n-1 // loop from 0 to n-1 // loop from 0 to j 101 if oding und Code Fragment 4.12: Some sample algorithms for analysis.
Expert Answer:
Answer rating: 100% (QA)
Lets analyze the time complexity and space complexity for each of the provided algor... View the full answer
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
The finance director of Park plc is preparing financial plans and different departments have submitted a number of capital investment applications. The managing director has said that no more than 1m...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
answer the question clearly You are building a flight-control system for which a convincing safety case must be made. Would you assign the tasks of safety requirements engineering, test case...
-
Tell whether the given side lengths form a right triangle. 8, 10, and 14
-
On February 1, 2018, the Xilon Corporation issued 50,000 shares of its nopar common stock in exchange for five acres of land located in the city of Monrovia. On the date of the acquisition, Xilon's...
-
We (generally) reject the null hypothesis when the test statistic falls into the rejection region, but we do not accept the null hypothesis when the test statistic does not fall into the rejection...
-
Each time myosin heads pull on actin, the sarcomere contracts only about 10 nm (10 -9 meter). Given that, how are you able to produce large motions with your muscles?
-
Dr. Edwin G. Hyde, a board-certified anesthesiologist, applied for permission to practice at East Jefferson Hospital in Louisiana. An approval was recommended for his hiring, but the hospitals board...
-
The company's $43 unit product cost is computed as follows: Direct materials Direct labor Variable manufacturing overhead Fixed manufacturing overhead ($425,000 + 25,000 units) Absorption costing...
-
Oasis Co., a U.S. shareholder, owns 100% of Shack Co. and 100% of Studio Co., both CFCs. Shack Co. has $300,000 of gross income, of which $50,000 is effectively connected income, and $30,000 is...
-
4 6.66 points eBook Print References Label the abdominal contents using the hints if provided. Liver Stoch Round ligament of liver Gallbladder Falciform ligament Greater omentum Lesser omentum Liver...
-
The figure below shows a large rectangle with four smaller rectangles enclosed within it. What is the perimeter value for one of the small rectangles? What is the perimeter value for the large...
-
5. (14 points) The current spot rate curve is flat at 5%. Suppose that 1-year spot rate and 3- year spot rate are chosen to be the only two key rates. Given change in the key rate(s), all spot rates...
-
b) Two equivalent point charges exert equal magnitudes of force on each other. However, if one point charge has twice the charge of the other, do they still exert equal magnitudes of force on each...
-
explain how the myth of "saving Afghan women from cruel Afghan men" has been used as a justification to launch the "War on Terrorism". Second, try to discuss how such rhetoric that depicts Islam as...
-
3. Suppose the average return of the S&P 500 over the last 50 years has been 7.8% with a standard deviation of 20% per year. Also assume the risk free rate is 3.5%. a. Find the optimal weight of the...
-
Calculate which set of conditions will provide the most effective spore destruction: 122C for 20 min, or 132C for 1 min. The Frequency factor and activation energy for spore destruction are 1.15x1046...
-
What is master production scheduling and how is it done?
-
Write a program that animates the bubble sort algorithm. Create an array that consists of 20 distinct numbers from 1 to 20 in a random order. The array elements are displayed in a histogram, as shown...
-
Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. Here are some sample runs: Enter the first string: Welcome to C++ Enter the...
-
Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan. A bank?s total assets are its current balance plus its loans to other banks....
-
Macquarie Manufacturing Ltd prepared the following planned production data for the forthcoming year ending 30 June 2019. Required (a) Prepare a table showing the predetermined factory overhead rate...
-
Beautiful Bottles Pty Ltd, bottle manufacturer for the food industry, has just installed a job order costing system. The company uses machine hours to apply its overhead to work in process. On 1 May...
-
Green Consultants Pty Ltd specialise in consulting on landscape design. The company developed a predetermined charge-out rate based on hours for each of its consultants on 1 July 2019 to assign the...
Study smarter with the SolutionInn App