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:

Spring NoSuchBeanDefinitionException
Daemon Threads in Java
Giới thiệu JDBC Connection Pool
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Find Nearest Neighbor for Dynamic Data Set
Guide to the Volatile Keyword in Java
Hướng dẫn Java Design Pattern – MVC
Automatic Property Expansion with Spring Boot
Java Program to Implement ArrayBlockingQueue API
Các kiểu dữ liệu trong java
Collect a Java Stream to an Immutable Collection
Converting Between a List and a Set in Java
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Construct an Expression Tree for an Infix Expression
Spring MVC Setup with Kotlin
Spring AMQP in Reactive Applications
Java Stream Filter with Lambda Expression
Java Program to Implement Stack using Two Queues
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Binomial Tree
Java Program to Implement Queue using Linked List
Hướng dẫn Java Design Pattern – Abstract Factory
Java – Convert File to InputStream
Spring @RequestMapping New Shortcut Annotations
A Guide to Spring Cloud Netflix – Hystrix
Spring WebClient Requests with Parameters
Java Program to Implement Expression Tree
Spring Boot - Building RESTful Web Services
Cài đặt và sử dụng Swagger UI