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...
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...
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...
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...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters and frequencies as listed below. The total number of nodes is n = 6.
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.)....
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.). Wikipedia has a nice high level description of the protocol. Consider only one level of cache which is a write back cache. Moreover, assume that there are 4 processing cores working on a single shared memory. To simplify, assume that you are writing the code for only one block of cache and that block can hold 4 different memory locations.
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT