Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
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

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.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education