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
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 iterative algorithm to find base r representation of a decimal number n. Analyse its timecomplexity using O, Ω, Θ.
Computer Science Design a divide-and-conquer algorithm for finding the minimum andthe maximum element of n numbers using no more than 3n/2comparisons.

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
    Systems Architecture
    Computer Science
    ISBN:9781305080195
    Author:Stephen D. Burd
    Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning