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:
How to Get a Name of a Method Being Executed?
Java Program to Solve Knapsack Problem Using Dynamic Programming
Count Occurrences of a Char in a String
Java Program to Implement JobStateReasons API
Default Password Encoder in Spring Security 5
What is a POJO Class?
Zipping Collections in Java
Java Program to Implement Sieve Of Sundaram
Hướng dẫn sử dụng lớp Console trong java
Spring AMQP in Reactive Applications
Java Program to Implement Counting Sort
Spring Boot - File Handling
Java Program to Find kth Largest Element in a Sequence
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Implement Bloom Filter
Java Program to Implement Interval Tree
Java Program to Implement Merge Sort Algorithm on Linked List
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Mệnh đề Switch-case trong java
Spring Cloud Bus
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java 8 Collectors toMap
Redirect to Different Pages after Login with Spring Security
A Guide to Java HashMap
A Quick Guide to Using Keycloak with Spring Boot
Check if there is mail waiting
Custom Cascading in Spring Data MongoDB
Java Program to Implement SimpeBindings API
Tránh lỗi ConcurrentModificationException trong Java như thế nào?