Problem statement:- Program to merging two unsorted arrays of different lengths.
Example:-
Input : Given
size=5
arr[]=[4,5,7,3,9]
size2=3
arr[]=[10, 11, 12]
Output:
merge_arr[]=[4, 10, 5, 11, 7, 12, 3, 9]
Data requirement:-
Input Data:- size, size2, arr, arr2
Output Data:-merge_arr
Additional Data:- i, k, j, p, m_size
Program in C
Input Data:- size, size2, arr, arr2
Output Data:-merge_arr
Additional Data:- i, k, j, p, m_size
Program in C
Here is the source code of the C Program to merging two unsorted arrays of different lengths.
Code:
#include<stdio.h>
void Merge_Array(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++;
}
}
int p;
printf("After Merge two Array element are:");
for(p=0;p<m_size;p++)
{
printf("%d ",merge_arr[p]);
}
}
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]);
}
Merge_Array(arr,arr2,size,size2);
}
Enter the size of the 1st array:5
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
4 5 7 3 9
Enter the Element of the 2nd array:
10 11 12
After Merge two Array element are:4 10 5 11 7 12 3 9
Program in C++
Here is the source code of the C++ Program to merging two unsorted arrays of different lengths.
Code:
#include<stdio.h>
void Merge_Array(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++;
}
}
int p;
printf("After Merge two Array element are:");
for(p=0;p<m_size;p++)
{
printf("%d ",merge_arr[p]);
}
}
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]);
}
Merge_Array(arr,arr2,size,size2);
}
Enter the size of the 1st array:4
Enter the size of the 2nd array:6
Enter the Element of the 1st array:
7 10 2 3
Enter the Element of the 2nd array:
15 9 20 21 6 4
After Merge two Array element are:7 15 10 9 2 20 3 21 6 4
Program in Java
Here is the source code of the Java Program to Merging two unsorted arrays of different lengths.
Code:
import java.util.Scanner;
public class MergeTwoArray {
static void Merge_Array(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++;
}
}
int p;
System.out.print("After Merge two Array element are:");
for(p=0;p<m_size;p++)
{
System.out.print(merge_arr[p]+" ");
}
}
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();
}
Merge_Array(arr,arr2,size,size2);
sc.close();
}
}
Enter the size of the array:
5
Enter the size of the 2nd array:
3
Enter the Element of the array:
7 9 3 4 5
Enter the Element of the array:
6 13 2
After Merge two Array element are:7 6 9 13 3 2 4 5
Program in Python
Here is the source code of the Python Program to Merging two unsorted lists of different lengths.
Code:
def Merge_Array(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
print("After Merge two Array element are:")
for p in range(0, m_size):
print(merge_arr[p],end=" ")
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)
Merge_Array(arr,arr2,size,size2)
Enter the size of the 1st array: 3
Enter the size of the 2nd array: 5
Enter the Element of the 1st array:
9
10
7
Enter the Element of the 2nd array:
8
5
6
3
2
After Merge two Array element are:
9 8 10 5 7 6 3 2
More:-
0 Comments
Please do not Enter any spam link in the comment box