Add OOP/Java/Lab/Week4/ArrayMerge.java
This commit is contained in:
parent
40105a6a7f
commit
3acb98a896
49
OOP/Java/Lab/Week4/ArrayMerge.java
Normal file
49
OOP/Java/Lab/Week4/ArrayMerge.java
Normal file
@ -0,0 +1,49 @@
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user