Problem Statement:- Program to Find the median of two unsorted arrays.
Example:-
Input : Given
size=6
arr[]=[4,3,7,5,6,2]
size=5
arr[]=[9,1,8,11,13]
/*merge_arr[]=arr[]+arr2[]
merge_arr[]=[4,9,3,1,7,8,5,11,6,13,2]*/
Output: median=6
/*sorted merge_arr[]=[1,2,3,4,5,6,7,8,9,11,13]
m_size=6+5=11//merge array size
median=(arr[11/2])
=(arr[5]=6*/
Data requirement:-
Input Data:- size, arr,size2,arr2
Output Data:-median
Additional Data:- i, j, k, m_size, merge_arr, temp
Program in C
Input Data:- size, arr,size2,arr2
Output Data:-median
Additional Data:- i, j, k, m_size, merge_arr, temp
Program in C
Here is the source code of the C Program to Find the median of two unsorted arrays.
Code:
#include<stdio.h>
void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
void Find_median(int arr[],int arr2[],int size,int size2)
{
int m_size=size+size2;
int merge_arr[m_size];
int i=0,k=0,j=0;
while(k<m_size)
{
if(i<size)
{
merge_arr[k]=arr[i];
i++;
k++;
}
if(j<size2)
{
merge_arr[k]=arr2[j];
j++;
k++;
}}
sort(merge_arr,m_size);
if(m_size%2==1)
{
double median=merge_arr[m_size/2];
printf("Median= %0.2lf",median);
}
else
{
double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
printf("Median= %0.2lf\n",median);
}
}
int main()
{
printf("Enter the size of the 1st array:");
int size,i;
scanf("%d",&size);
int arr[size];
printf("Enter the size of the 2nd array:");
int size2;
scanf("%d",&size2);
int arr2[size2];
printf("Enter the Element of the 1st array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the Element of the 2nd array:\n");
for(i=0;i<size2;i++)
{
scanf("%d",&arr2[i]);
}
Find_median(arr,arr2,size,size2);
}
Enter the size of the 1st array:6
Enter the size of the 2nd array:5
Enter the Element of the 1st array:
4 3 7 5 6 2
Enter the Element of the 2nd array:
9 1 8 11 13
Median= 6.00
Program in C++
Here is the source code of the C++ Program to Find the median of two unsorted arrays.
Code:
#include<iostream>
using namespace std;
void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
void Find_median(int arr[],int arr2[],int size,int size2)
{
int m_size=size+size2;
int merge_arr[m_size];
int i=0,k=0,j=0;
while(k<m_size)
{
if(i<size)
{
merge_arr[k]=arr[i];
i++;
k++;
}
if(j<size2)
{
merge_arr[k]=arr2[j];
j++;
k++;
}}
sort(merge_arr,m_size);
if(m_size%2==1)
{
double median=merge_arr[m_size/2];
cout<<"Median= "<<median;
}
else
{
double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
cout<<"Median= "<<median;
}
}
int main()
{
cout<<"Enter the size of the 1st array:";
int size,i;
cin>>size;
int arr[size];
cout<<"Enter the size of the 2nd array:";
int size2;
cin>>size2;
int arr2[size2];
cout<<"Enter the Element of the 1st array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"Enter the Element of the 2nd array:\n";
for(i=0;i<size2;i++)
{
cin>>arr2[i];
}
Find_median(arr,arr2,size,size2);
}
Enter the size of the 1st array:4
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
6 9 5 8
Enter the Element of the 2nd array:
7 3 2
Median= 6
Program in Java
Here is the source code of the Java Program to Find the median of two unsorted arrays.
Code:
import java.util.Scanner;
public class SortedArrayMedian {
static void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
static void Find_median(int arr[],int arr2[],int size,int size2)
{
int m_size=size+size2;
int merge_arr[ ]=new int[m_size];
int i=0,k=0,j=0;
while(k<m_size)
{
if(i<size)
{
merge_arr[k]=arr[i];
i++;
k++;
}
if(j<size2)
{
merge_arr[k]=arr2[j];
j++;
k++;
}}
sort(merge_arr,m_size);
if(m_size%2==1)
{
double median=merge_arr[m_size/2];
System.out.println("Median= "+median);
}
else
{
double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
System.out.println("Median= "+median);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,j=0;
size=sc.nextInt();
System.out.println("Enter the size of the 2nd array:");
int size2=sc.nextInt();
int arr[ ]=new int[size];
int arr2[ ]=new int[size2];
System.out.println("Enter the Element of the array:");
for(j=0;j<size;j++)
{
arr[j]=sc.nextInt();
}
System.out.println("Enter the Element of the array:");
for(j=0;j<size2;j++)
{
arr2[j]=sc.nextInt();
}
Find_median(arr,arr2,size,size2);
sc.close();
}
}
Enter the size of the array:
5
Enter the size of the 2nd array:
4
Enter the Element of the array:
8 9 3 7 14
Enter the Element of the array:
12 5 2 6
Median= 7.0
Program in Python
Here is the source code of the Python Program to Find the median of two unsorted arrays.
Code:
def Find_median(arr,arr2,size,size2):
m_size = size + size2
merge_arr = [0]*m_size
i=0
k=0
j=0
while k<m_size:
if i<size:
merge_arr[k] = arr[i]
i+=1
k+=1
if j<size2:
merge_arr[k] = arr2[j]
j+=1
k+=1
merge_arr.sort()
if size % 2 == 1:
median = merge_arr[size // 2]
print("\nMedian= ", median)
else:
median = (merge_arr[m_size // 2] + (merge_arr[(m_size // 2) - 1])) / 2.0
print("\nMedian= ", median)
arr=[]
arr2=[]
size = int(input("Enter the size of the 1st array: "))
size2 = int(input("Enter the size of the 2nd array: "))
print("Enter the Element of the 1st array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Enter the Element of the 2nd array:")
for i in range(0,size2):
num2 = int(input())
arr2.append(num2)
Find_median(arr,arr2,size,size2)
Enter the size of the 1st array: 3
Enter the size of the 2nd array: 3
Enter the Element of the 1st array:
4
9
5
Enter the Element of the 2nd array:
3
6
20
Median= 4
0 Comments
Please do not Enter any spam link in the comment box