Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays

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:

Life Cycle of a Thread in Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement TreeSet API
Giới thiệu Design Patterns
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Expression Tree
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
A Guide to LinkedHashMap in Java
Mapping a Dynamic JSON Object with Jackson
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Spring Boot - Sending Email
Set Interface trong Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Receive email by java client
Từ khóa throw và throws trong Java
Guide to java.util.Formatter
Check if a String is a Palindrome in Java
Spring Boot - Unit Test Cases
Java Program to Implement Find all Back Edges in a Graph
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Guide to CountDownLatch in Java
Java – Try with Resources
Guide to Java 8 groupingBy Collector
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Check whether Undirected Graph is Connected using DFS
Guide to the Synchronized Keyword in Java
Simultaneous Spring WebClient Calls
Template Engines for Spring
Spring Boot Configuration with Jasypt
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Getting Started with Custom Deserialization in Jackson