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

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