Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Bartleby Related Questions Icon
Related questions
Question
Transcribed Image Text:For this task, save your work in MinMax.java
Consider the following problem: given an array of n numbers, we want to find both the minimum
and the maximum of these numbers. For such a problem, we often measure the cost in terms of the
number of comparisons made-that is, if we compare any two numbers from the input, that's one
comparison.
As an example, the following algorithm requires n-1 comparisons:
// assume a.length > 0
int maxArray(int[] a) {
int maxSoFar = a[0];
for (int i=1;i<a.length; i++) {
}
}
if (a[i]
maxSoFar)
maxSoFar = a[i]%;B
return maxSoFar;
This is because in an array a of length n, only a[1], a[2],..., a[n-1] are compared with our maxSoFar
in the if statement. Notice that a[0] is not involved in the if statement.
You can use this algorithm to the find the maximum value and an almost-identical algorithm to
find the minimum value. However, you'll need 2n-2 comparisons (n-1 for max and another n - 1 for
min). Only comparisons between input integers (either directly or indirectly) matter here, which is why
we don't count comparisons made by i<a.length in the above example. Your goal in this problem is to
do better!
Subtask I: First, implement a function
public static double minMaxAverage (int[] numbers) {
// your code goes here
int myMin = ...;
int myMax =
return (myMin + myMax)/2.0;
}
that takes in an array of integer numbers, finds the minimum and the maximum among these numbers,
and returns the average of the minimum and the maximum (as the code above shows). For full credit, if
input contains n numbers, your function must use fewer than 3n/2 comparisons.
Subtask II: As a comment block in your code file, make a logical argument-as close to an airtight
mathematical proof as possible-for why your code is indeed using strictly fewer than 3n/2 comparisons.
(Hint: Remember the maximum-number problem from class? What happens after one round in the
pairing-up algorithm?)
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
Step by stepSolved in 2 steps
Knowledge Booster
Background pattern image
Similar questions
- O Design an algorithm to find the maximum element in a list of n unique numbers. Give a trace for get MAX(A,n), where A={1,5,2,9,4,3} and n=6. Design a recurrence relation for the algorithm and find its time complexity. Give a proof of correctness.arrow_forwardWrite a program in java using two dimensional arrays. You should prompt the user toenter the number of rows and columns for your matrix. You should then prompt the userto enter values for each element of the matrix. Once the user has provided all the values for matrix, you should print the matrix and transpose of that matrix. A transpose of a matrix converts rows to columns and columns to rows.Example:Output: This program transposes a matrix.Output: Please enter the number of rows:User enters 2Output: Please enter the number of columns:User enters 3Enter value for row[0] column[0]: 9Enter value for row[0] column[1]: 1Enter value for row[0] column[2]: 2Enter value for row[1] column[0]: 72Enter value for row[1] column[1]: 3Enter value for row[1] column[2]: 6The matrix you entered is:9 1 272 3 6The transpose of this matrix has 3 rows and 2 columns and the transpose is:9 721 32 6arrow_forwardWrite a Java program that reads an integer N from the user and declares an integer array of size N. It then inputs N non-negative integers from the user into the array and do the following: 1. Finds and prints the minimum, maximum, sum, and average of the numbers. 2. Displays array data as a histogram – graphically by plotting each numeric value as a bar of asterisks (+) as shown in the example diagram. This is somehow similar to Lab11 Sample output Enter the size of array: 9 Enter 9 integer values: 10 19 5 14 Minimum = Maximum = 19 = 68 = 7.55 Sum Average ========== Histogram: ======= Element Ua lue 10 19 Histogran T T T Arial v T = ABC 3 (12pt) II Comarrow_forward
Recommended textbooks for you
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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