-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmergeSortedArray.java
55 lines (45 loc) · 1.38 KB
/
mergeSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class mergeSortedArray{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("enter size of first array: ");
int m = sc.nextInt();
System.out.println("enter size of second array: ");
int n = sc.nextInt();
int[] nums1 = new int[n+m];
System.out.println("enter elements of first array : ");
for(int i=0 ; i<m ; i++){
nums1[i] = sc.nextInt();
}
int[] nums2 = new int[n];
System.out.println("enter elements of second array : ");
for(int i=0 ; i<n ; i++){
nums2[i] = sc.nextInt();
}
sc.close();
merge(nums1 , m , nums2 , n);
System.out.println("The merged array is : ");
for(int i=0 ; i<n+m ; i++){
System.out.println(nums1[i]);
}
}
public static void merge(int[] nums1, int m, int[] nums2, int n) {
// three pointer approach
int pos = m + n - 1;
while(m > 0 && n > 0){
if(nums1[m - 1] > nums2[n - 1]){
nums1[pos] = nums1[m - 1];
m--;
} else {
nums1[pos] = nums2[n - 1];
n--;
}
pos--;
}
while(n > 0){
nums1[pos] = nums2[n - 1];
n--;
pos--;
}
}
}