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 42C

Explanation of Solution

Algorithm:

findMax(Array A, size n>0):

    max=A[0]

    for i in 1 to n (inclusive)

    if(A[i] > max)

        max=A[i]

    return max

Explanation:

The above algorithm uses a binary tree where every node keeps track of the maximum and minimum for the sub tree starting with that node.

Let us consider the three types as the following implementation to prove the maximum and minimum of “n” numbers. Type-1 contains less than “3n/2” comparisons, type-2 contains “n/2 ” nodes that have leaves as children, and type-3 contains less than “n/2” nodes where a least one of the children aren't leaves.

  • First, start with the type 2 nodes, and compare the two leaves. This requires a maximum of “n/2” comparisons. If there are an odd number of type 1 nodes, the last node is promoted to type 2 and only it uses (n1)/2 comparisons.
  • Now there are “n/2 ” or “(n 1)/2 + 1 ”type 2 nodes.
  • Let us take “m” be the number of nodes and keep pairing the nodes until only one node is left. In each iteration, it requires two comparisons while pairing two nodes

Blurred answer
Students have asked these similar questions
Derive an algorithm to find the sum of all even numbers up to N.
One-dimensional range searching can be done with O(N log N) steps for preprocessing and O(R+log N) for range searching, where R is the number of points actually falling in the range.
Write an algorithm to find the maximum and the minimum elements for a set of n integers

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
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