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 5, Problem 1R

Explanation of Solution

Recursive method to find the maximum element in array:

The recursive method to find the maximum element in array “A” of “n” elements is given below:

The findMax() method returns the maximum elements in array.

//Define the findMax() method

public static int findMax(int[] a, int n)

{

  //Check whether the "n" is equal to "1"

  if (n == 1)

  //Return the maximum element

  return a[0];

/*Call the max() method and recursively call findMax() method*/

return max(finMax(a, n - 1), a[n - 1]);

}

The max() method returns the maximum elements by comparing the array elements.

//Define the max() method

private static int max(int n1, int n2)

{

/*Check the "n1" is greater than "n2". If yes, return the maximum element. */

  return n1 > n2 ? n1 : n2;

}

Explanation:

In the above code,

  • Check whether the “n” element is equal to “1”. If yes,
    • It return the array of “a[0]”. That is, first element is a maximum element in given array “a”.
  • Call the max() method to compare the array elements by calling the findMax() method recursively to find and return the maximum array element.

Running time:

The running time of above code is given below:

According this code,

  • The basic operation is comparison. That is, comparing the array elements recursively to find the maximum element in array.
  • If there is no comparison. Then, the maximum array element is “a[0]” is “1”.
  • Compare array value until “n-1” by calling the findMax() method. Then,

  a(n) = n-1+1         =n

  • Then, the array value is “a[n]” is “n”.
  • Thus, it will recursively call the findMax() method to perform the “n” time of execution for “n” inputs. So, the usage space and time for this “n” array is O(n).

Therefore, the running time and space of maximum element in array is O(n).

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
04:00
Students have asked these similar questions
Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
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
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning