-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddtwoarray.java
More file actions
84 lines (69 loc) · 2.25 KB
/
addtwoarray.java
File metadata and controls
84 lines (69 loc) · 2.25 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.Scanner;
import java.util.Arrays;
import java.util.Scanner;
public class addtwoarray {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the input for first array: ");
int n=sc.nextInt();
int [] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the input for second array: ");
int m=sc.nextInt();
int [] arr1=new int[m];
for(int j=0;j<m;j++){
arr1[j]=sc.nextInt();
}
int [] arr3=new int[n+m];
int i=0;
int j=0;
int k=0;
while(j<m && i<n){
if(arr[i]<arr1[j]){
arr3[k]=arr[i];
i++; k++;
}
else{
arr3[k]=arr1[j];
j++; k++;
}
}
while (i<n) {
arr3[k]=arr[i];
i++;
k++;
}
while (j<m) {
arr3[k]=arr1[j];
j++;
k++;
}
System.out.println("Merged array is : ");
for(i=0;i<n+m;i++){
System.out.print(arr3[i]+" ");
}
System.out.println();
// Bubble sort to sort the merged array
// for (int i = 0; i < n + m; i++) {
// for (int j = i + 1; j < n + m; j++) {
// if (arr3[i] > arr3[j]) {
// int temp = arr3[i];
// arr3[i] = arr3[j];
// arr3[j] = temp;
// }
// }
// }
// System.out.println("\nSorted array is : ");
// for(int i=0;i<n+m;i++){
// System.out.print(arr3[i]+" ");
// }
/*Another way of sorting in java*/
// Arrays.sort(arr3);
// System.out.println("sorted array is : ");
// for(int i=0;i<n+m;i++){
// System.out.print(arr3[i]+" ");
// }
}
}