Question

In: Computer Science

Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...

Programming language: JAVA

First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list.

Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.

Solutions

Expert Solution


import java.util.Random;


public class Max_Min {
static int min,max;
//recursive dived and conquer method to find max,min
static void max_min_rec(int a[],int l,int r)
{
if(l==0)
{
max=min=a[l];
}
if(l==r)
{
if(a[l]<min)min=a[l];
if(max<a[l])max=a[l];
  
}
if(l<r){
max_min_rec(a,l,(l+r)/2);
max_min_rec(a,((l+r)/2)+1,r);   
}
}
//iterative method
static void max_min_iter(int a[],int n)
{
max=min=a[0];
for(int i=0;i<n;i++)
{
if(a[i]<min)min=a[i];
if(max<a[i])max=a[i];
}
  
}
public static void main(String argv[])
{
int a[] = new int[10];
Random r = new Random();
for(int i=0;i<10;i++)//generating random numbers between 1,1000000
{ a[i]=r.nextInt()%1000000 +1;
if(a[i]<0)a[i]=a[i]*-1;
}
for(int i=0;i<10;i++)
System.out.print(a[i]+" ");
max_min_rec(a,0,a.length-1);
System.out.println("\nMax:"+max+",Min:"+min);
max_min_iter(a,a.length);
System.out.println("Max:"+max+",Min:"+min);
  
  
}
}

output:

run:
833985 470760 73268 801787 743744 549690 236702 326090 801714 459173
Max:833985,Min:73268
Max:833985,Min:73268
BUILD SUCCESSFUL (total time: 0 seconds)


Related Solutions

LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
The following submission rules apply:·    For those questions requiring programs, the solutions must be implemented using JavaScript or Java.o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.o Solutions must be provided in plain text so that formatting is not lost.·    All answers must be provided in this document.·    Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.Design and analyze a divide-and-conquer algorithm for finding the maximum...
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm....
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm. The following requirements should meet: a The program shall graphically prompt the user for a file. bThe program shall read the selected file which will contain 1 integer per line. c. The program shall sort the values it reads from the file from largest to smallest. d.The program shall write the values to an output file from largest to smallest in the same directory...
Java programming Trace the algorithm for minimum spanning tree (eager, lazy Prim algorithm)
Java programming Trace the algorithm for minimum spanning tree (eager, lazy Prim algorithm)
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
***In Java language***The first programming project involves writing a program that computes the salaries for a...
***In Java language***The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be initialized. b. A method named annualSalary that returns the salary for a whole...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Choose one of the following cryptography techniques and implement it using (java )programming language. Your program...
Choose one of the following cryptography techniques and implement it using (java )programming language. Your program should provide the user with two options Encryption and Decryption, with a simple UI to get the input from the user, and view the result. You can use any restriction you need for the user input but you need to clarify that and validate the user input base on your restriction. ● Feistel ● Keyword columnar ● Any cryptosystem of your choice (needs to...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x and y, as parameters and returns the value xy (x raised to the power y). The exponent must be non-negative. If a negative argument is given for the exponent, then an exception should be thrown. Implement a recursive method called "fib" that takes a positive integer, n, as a parameter and returns the nth Fibonacci value. Assume that the first 2 values in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT