Question

Write a program to merge two sorted arrays into a third array. Ask the user to...

Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note: You may not create the third array and then sort it. The two arrays passed as arguments must be sorted. CSCI 125 Array exercises Prof. Kadri

To sort an array, java.util.Arrays is a library that has a method sort that sorts an array. To use it, you will need to import java.util.Arrays. Arrays.sort(array); // will sort array in ascending order

Example: Enter the size of array1: 10

Enter the size of array2: 4

Array1: 7 2 3 8 6 6 75 38 3 2

Array2: 5 4 3 2

Sort the two arrays Array1: 2 2 3 3 6 6 7 8 38 75 Array2: 2 3 4 5 Merging Array3: 2 2 2 3 3 3 4 5 6 6 7 8 38 75

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program:

import java.util.*;

public class Main
{
   //Main function
public static void main(String args[])
   {
       //Random object
       Random rand = new Random();
      
       //Declaring size variables
       int arr1Size, arr2Size;
      
       //Scanner class object
       Scanner reader = new Scanner(System.in);
      
       //Reading size
       System.out.print("Enter the size of array1: ");
       arr1Size = reader.nextInt();
      
       System.out.print("Enter the size of array2: ");
       arr2Size = reader.nextInt();
      
       //Declaring arrays
       int[] arr1 = new int[arr1Size];
       int[] arr2 = new int[arr2Size];
      
       //Filling arrays with random numbers
       for(int i=0; i<arr1Size; i++)
       {
           arr1[i] = rand.nextInt(100);  
       }
      
       //Filling arrays with random numbers
       for(int i=0; i<arr2Size; i++)
       {
           arr2[i] = rand.nextInt(100);  
       }
      
       System.out.println("\n Array1 : " + Arrays.toString(arr1));
       System.out.println("\n Array2 : " + Arrays.toString(arr2));
      
       //Sorting arrays
       Arrays.sort(arr1);
       Arrays.sort(arr2);
      
       //Result array
       int[] res = new int[arr1Size + arr2Size];
      
       //Calling merge function
       res = merge(arr1, arr1Size, arr2, arr2Size);
      
       //Printing results
       System.out.println("\nSort the two arrays\n");
       System.out.println("\n Array1 : " + Arrays.toString(arr1));
       System.out.println("\n Array2 : " + Arrays.toString(arr2));
       System.out.println("\n Merging Array3 : " + Arrays.toString(res) + "\n");
   }
  
   //Method that merges two arrays
   public static int[] merge(int[] firstList, int arr1Size, int[] secondList, int arr2Size)
   {
       int i, j, k=0;
      
       //Result
       int[] mergedList = new int[arr1Size + arr2Size];
      
       //Iterating over list elements
       for(i=0, j=0; i<arr1Size && j<arr2Size;)
       {
           //Comparing elements and then storing in resultant list
           if(firstList[i] < secondList[j])
           {
               mergedList[k] = firstList[i];
               i++;
               k++;
           }
           else
           {
               mergedList[k] = secondList[j];
               j++;
               k++;
           }
       }
      
       //Storing remaining elements
       while (i < arr1Size)
       {
           mergedList[k] = firstList[i];
           i++;
           k++;
       }
      
       //Storing remaining elements
       while (j < arr2Size)
       {
           mergedList[k] = secondList[j];
           j++;
           k++;
       }
          
       return mergedList;
   }
}

_______________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Write a program to merge two sorted arrays into a third array. Ask the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...

    in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){

  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0,...

    i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0, -56, 4, -7 and array2-19, 12, -36, -2, 12 3. Write the code to form and display another array array3 in which each element is the sum of numbers in the position in both arrays. Use pointers and functions: get array0. process arrays0 and show array0 (50 points) 2. Enter a positive 5-digit integer via the keyboard. Display each digit and fol- lowed by...

  • *c language* Write a program that takes two sorted array A,B of size m,n respectively where...

    *c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C.   For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2]   then C=[1,2,4,4,5,6,7,8,8,9,10].

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Q1) Using Divide and conquer approach, solve the kth element in 2 sorted arrays problem. Given...

    Q1) Using Divide and conquer approach, solve the kth element in 2 sorted arrays problem. Given two sorted arrays both of size n find the element in k’th position of the combined sorted array. 1. Mention the steps of Divide, Conquer and Combine (refer to L5- Divide and Conquer Lecture notes, slide 3, to see an example on merge sort) 2. Draw the recursive tree. 3. What is the recurrence equation? 4. Guess a solution based on the recursive tree...

  • please write me this program in C language Review UW files from the Internet can contain...

    please write me this program in C language Review UW files from the Internet can contain viruses. Unless you need to edit, it's safer to stay in Protected View View View - Word Terme MOHAMMED MUNTHER MOH Enable Editing Task # 3: Consider two integer arrays x and y of same size n with values: Xo, X1, X3,..., Xe1 and yo, yi, ya..... Yn-1 Write a function merge that receives array x and y and their size. It then returns...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT