50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
import java.util.Scanner;
|
|
|
|
class ArrayMerge {
|
|
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
System.out.print("Enter the size of the first array: ");
|
|
int size1 = sc.nextInt();
|
|
int[] array1 = new int[size1];
|
|
System.out.println("Enter the elements of the first array:");
|
|
for (int i = 0; i < size1; i++) {
|
|
array1[i] = sc.nextInt();
|
|
}
|
|
|
|
System.out.print("Enter the size of the second array: ");
|
|
int size2 = sc.nextInt();
|
|
int[] array2 = new int[size2];
|
|
System.out.println("Enter the elements of the second array:");
|
|
for (int i = 0; i < size2; i++) {
|
|
array2[i] = sc.nextInt();
|
|
}
|
|
|
|
int[] mergedArray = new int[size1 + size2];
|
|
for (int i = 0; i < size1; i++) {
|
|
mergedArray[i] = array1[i];
|
|
}
|
|
for (int i = 0; i < size2; i++) {
|
|
mergedArray[size1 + i] = array2[i];
|
|
}
|
|
|
|
for (int i = 0; i < mergedArray.length - 1; i++) {
|
|
for (int j = 0; j < mergedArray.length - i - 1; j++) {
|
|
if (mergedArray[j] > mergedArray[j + 1]) {
|
|
int temp = mergedArray[j];
|
|
mergedArray[j] = mergedArray[j + 1];
|
|
mergedArray[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
System.out.println("Merged and sorted array:");
|
|
for (int num : mergedArray) {
|
|
System.out.print(num + " ");
|
|
}
|
|
|
|
sc.close();
|
|
}
|
|
}
|