This is a java program to find the median from two different array. To do so we merge the two lists and then sort them, after that we find the median of the sequence. If the total number of elements (N) is odd median is the N/2th element, if its even (N-1/2 + N/2)/2th element.
Here is the source code of the Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to find the median of 2 array
import java.util.Random;
public class Median_Two_Arrays
{
static int N = 10, M = 5;
static int[] sequence1 = new int[N];
static int[] sequence2 = new int[M];
static int[] sequence = new int[N+M];
public static void sort()
{
int i, j, temp;
for (i = 1; i < N+M; i++)
{
j = i;
temp = sequence[i];
while (j > 0 && temp < sequence[j - 1])
{
sequence[j] = sequence[j - 1];
j = j - 1;
}
sequence[j] = temp;
}
}
public static void main(String args[])
{
Random random = new Random();
for(int i=0; i<N; i++)
sequence1[i] = Math.abs(random.nextInt(100));
for(int i=0; i<M; i++)
sequence2[i] = Math.abs(random.nextInt(100));
for(int i=0; i<N; i++)
System.out.print(sequence1[i] + " ");
System.out.println();
for(int i=0; i<M; i++)
System.out.print(sequence2[i] + " ");
System.out.println();
int j=0;
for(int i=0; i<N+M; i++)
{
if(i >= N && j < M)
sequence[i] = sequence2[j++];
else
sequence[i] = sequence1[i];
}
sort();
if(N+M % 2 == 0)
System.out.println("The Median is : " + (sequence[(N+M)/2-1]+sequence[(N+M)/2])/2);
else
System.out.println("The Median is : " + sequence[(N+M)/2]);
}
}
Output:
$ javac Median_Two_Arrays.java $ java Median_Two_Arrays 92 53 68 15 17 23 95 47 46 61 63 62 48 66 26 The Median is : 53
Related posts:
New Features in Java 10
Copy a List to Another List in Java
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Check the Connectivity of Graph Using BFS
HandlerAdapters in Spring MVC
How to Get a Name of a Method Being Executed?
Guide to ThreadLocalRandom in Java
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
How to Round a Number to N Decimal Places in Java
Spring Boot Security Auto-Configuration
Kết hợp Java Reflection và Java Annotations
Daemon Threads in Java
Flattening Nested Collections in Java
Java Program to Describe the Representation of Graph using Incidence Matrix
Adding Parameters to HttpClient Requests
Spring REST API with Protocol Buffers
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Jackson – JsonMappingException (No serializer found for class)
Getting the Size of an Iterable in Java
Java – Generate Random String
A Guide to BitSet in Java
How to Change the Default Port in Spring Boot
Jackson – Bidirectional Relationships
How to Set TLS Version in Apache HttpClient
HttpClient with SSL
Java Program to Implement Pollard Rho Algorithm
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Working with Network Interfaces in Java
Spring Boot - Cloud Configuration Client
Hướng dẫn Java Design Pattern – Dependency Injection