Merge two sorted arrays
- Get link
- X
- Other Apps
// Java program to merge two sorted arrays
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
MergeTwoSorted
{
// Merge arr1[0..n1-1] and arr2[0..n2-1]
// into arr3[0..n1+n2-1]
public
static
void
mergeArrays(
int
[] arr1,
int
[] arr2,
int
n1,
int
n2,
int
[] arr3)
{
int
i =
0
, j =
0
, k =
0
;
// Traverse both array
while
(i<n1 && j <n2)
{
// Check if current element of first
// array is smaller than current element
// of second array. If yes, store first
// array element and increment first array
// index. Otherwise do same with second array
if
(arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
// Store remaining elements of first array
while
(i < n1)
arr3[k++] = arr1[i++];
// Store remaining elements of second array
while
(j < n2)
arr3[k++] = arr2[j++];
}
public
static
void
main (String[] args)
{
int
[] arr1 = {
1
,
3
,
5
,
7
};
int
n1 = arr1.length;
int
[] arr2 = {
2
,
4
,
6
,
8
};
int
n2 = arr2.length;
int
[] arr3 =
new
int
[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
System.out.println(
"Array after merging"
);
for
(
int
i=
0
; i < n1+n2; i++)
System.out.print(arr3[i] +
" "
);
}
}
*********************************************
// Java program to merge two sorted arrays
//using maps
import
java.io.*;
import
java.util.*;
class
GFG {
// Function to merge arrays
static
void
mergeArrays(
int
a[],
int
b[],
int
n,
int
m)
{
// Declaring a map.
// using map as a inbuilt tool
// to store elements in sorted order.
Map<Integer,Boolean> mp =
new
HashMap<Integer,Boolean>();
// Inserting values to a map.
for
(
int
i =
0
; i < n; i++)
{
mp.put(a[i],
true
);
}
for
(
int
i =
0
;i < m;i++)
{
mp.put(b[i],
true
);
}
// Printing keys of the map.
for
(Map.Entry<Integer,Boolean> me : mp.entrySet())
{
System.out.print(me.getKey() +
" "
);
}
}
// Driver Code
public
static
void
main (String[] args)
{
int
a[] = {
1
,
3
,
5
,
7
}, b[] = {
2
,
4
,
6
,
8
};
int
size = a.length;
int
size1 = b.length;
// Function call
mergeArrays(a, b, size, size1);
}
}
- Get link
- X
- Other Apps
Comments
Post a Comment