0% found this document useful (0 votes)
4 views2 pages

Problem Merging Arrays + Sorting

Uploaded by

mtekhoury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Problem Merging Arrays + Sorting

Uploaded by

mtekhoury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Problem merging arrays + sorting

Write a program called MergeSortedArrays that reads first from the user an int
value that will serve as the size for two different arrays, denoted by arr1 and
arr2, respectively. Then, the elements of these two arrays should be obtained one
by one from the end user.

Your program should finally perform the following tasks:

It should first sort the elements of arr1 and arr2 in an ascending order. Make sure
to print the elements of the sorted arrays after the sorting takes place.
It should then merge the elements of the two sorted arrays into one aggregate array
where the elements would also be arranged in an ascending order. That is, arr1 and
arr2 should give rise to a new array called sortedArray storing the elements of
arr1 and arr2 in ascending order.
After the merger of the elements of arr1 and arr2 occurs, the elements of the
resulting sortedArray should be printed out, as illustrated in the sample output
below.

Sample run:

Enter size of input arrays: 2


Enter Element#0 for first array: 3
Enter Element#1 for first array: 1
Elements of first array: 3 1
Elements of first array after sorting: 1 3
Enter Element#0 for second array: 4
Enter Element#1 for second array: 2
Elements of second array: 4 2
Elements of second array after sorting: 2 4
Elements of final sorted array, after merging 2 input sorted arrays: 1 2 3 4

Code:

package finalpractice;
import java.util.Arrays;
import java.util.Scanner;
public class notsosad {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter size of input arrays");
int size = scanner.nextInt();

int [] arr1 = new int [size];


int [] arr2 = new int [size];

for(int i = 0; i< arr1.length; i++) {


System.out.println("Enter element " + i + " for first array: ");
arr1[i] = scanner.nextInt();

}
System.out.println("Elements of first array: " +
Arrays.toString(arr1));

Arrays.sort(arr1);

System.out.println("Elements of first array after sorting: " +


Arrays.toString(arr1));

for(int i = 0; i< arr2.length; i++) {


System.out.println("Enter element " + i + " for second array: ");
arr2[i] = scanner.nextInt();

System.out.println("Elements of second array: " +


Arrays.toString(arr2));
Arrays.sort(arr2);
System.out.println("Elements of second array after sorting: " +
Arrays.toString(arr2));

int [] mergedArray = new int [arr1.length + arr2.length];


System.arraycopy(arr1, 0, mergedArray, 0, arr1.length);
System.arraycopy(arr2, 0, mergedArray, arr1.length, arr2.length);

Arrays.sort(mergedArray);
System.out.println("Elements of final sorted array, after merging 2
input sorted array: " + Arrays.toString(mergedArray));

}}

You might also like