Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 4, Problem 60P

Explanation of Solution

Experimental analysis for prefixAverage1:

With the reference to the textbook, the prefixAverage1 algorithm computes the prefix averages in quadratic time O(n2). Now let us take the given algorithm from the textbook for analysis to draw the visualization representation as follows:

Algorithm: prefixAverage1(X,n)

Input: array x on n integers

Output: Array A of prefix averages of X

//Line1

A<- new array of n integers

//Line2

for i<-0 to n-1 do

  //Line3

  S<-X[0]

  //Line4

  for j<-1 to i do

  //Line5

  S<-S+X[j]

  // Line6

  A[i]<-s/(i+1)

//Line7

return A

Explanation:

In the above algorithm,

  • The Line1 is to initialize the array of “n” integers and it takes O(n) time.
  • Next the Line2 is the outer for loop which act as counter and executed “n” number of times and it takes O(n) times to complete the loop.
  • The Line3 is assigning the initial element of array to variable “s” and this executes in O(n) time.
  • Line4 is the inner body of for loop executes based on the outer for loop and it follows sum up operation such as 1+2+3+…(n-1). So, this line executes in O(n2) time.
  • Similar to Line4 and Line 5 follows same running time O(n2) because it performs sum up operation inside the for loop.
  • On Line6, it calculates the averages for the sum of elements and this is executed in O(n) time because the operation is directly proportional.
  • On Line7, the value is returned and it is executed in constant time O(1).

Visualization representation of algorithm prefixAverage1 in log-log graph:

As the running time of prefixAverage1 is O(1+2++n) and the sum of first “n” integers is n(n+1)2

Blurred answer
Students have asked these similar questions
Implement a quicksort with a sample size of 2k 1. Sort the sample first, then have the recursive procedure partition on the sample's median and shift the two halves of the rest of the sample to each subarray so that they may be utilised in the subarrays without having to be sorted again. This algorithm is known as samplesort. Put into practise a quicksort based on a 2k sample. The sample should be sorted first, after which you should set up the recursive procedure to split the sample based on its median and to shift the two halves of the remaining sample to each subarray so they can be utilised in the subarrays without needing to be sorted again. The name of this algorithm is samplesort.
Apply algorithm 4.3.2 in P.155 with n=6 and A=(3,5,4,1,3,2). Draw only the right half of the corresponding walkthrough as shown in P.155, showing only the list A at the end of the outer “for” loop.
Perform an experimental analysis on the two algorithms & Visualize their running times as a function of the input size with a log-log chart. 1. prefixAverage1 2. prefixAverage2. I need code for experimental analysis and visualization in java.

Chapter 4 Solutions

Data Structures and Algorithms in Java

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning