Question

Show the merge of two unsorted arrays in Java N size of array M size of...

Show the merge of two unsorted arrays in Java
N size of array
M size of array

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

UnsortedArrayMerge.java

public class UnsortedArrayMerge {

public static void main(String[] args) {

int [] array1= {34,56,23,9,12,89,45};

int [] array2= {54,16,93,9,3,67};

int [] mergedArray = new int [array1.length + array2.length];

//Displaying array1

System.out.println("Array 1");

printArray(array1);

//Displaying array2

System.out.println("Array 1");

printArray(array2);

//adding all the elements nto single array

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

mergedArray[i] = array1[i];

}

int count = array1.length;

for(int j=0 ; j<array2.length ; j++){

mergedArray[count] = array2[j];

count++;

}

//Displaying mergedArray

System.out.println("Merged Array");

printArray(mergedArray);

//calling sort

int [] sortedArray = sortArray(mergedArray);

//Displaying sortedArray

System.out.println("Sorted Array");

printArray(sortedArray);

}

//To sort array

public static int[] sortArray(int [] arrayTosort){

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

for (int j = i + 1; j < arrayTosort.length; j++) {

int temp = 0;

if (arrayTosort[i] > arrayTosort[j]) {

temp = arrayTosort[i];

arrayTosort[i] = arrayTosort[j];

arrayTosort[j] = temp;

}

}

}

return arrayTosort;

}

//To print array

public static void printArray(int [] arr){

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

System.out.print( arr[i]+" ");

}

System.out.println();

}

}

Sample output:

Array 1
34 56 23 9 12 89 45
Array 1
54 16 93 9 3 67
Merged Array
34 56 23 9 12 89 45 54 16 93 9 3 67
Sorted Array
3 9 9 12 16 23 34 45 54 56 67 89 93

Add a comment
Know the answer?
Add Answer to:
Show the merge of two unsorted arrays in Java N size of array M size of...
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 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:...

  • *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].

  • 10 L 1 22 Given the function below, that divides the original array in two arrays...

    10 L 1 22 Given the function below, that divides the original array in two arrays of half size, develop the Java code that combines two sorted arrays or clearly explain how to implement the merge function. (3 points) nt middle values.Length/2; // divide array into two arrays of half size nt left- new int[middle]; or Cint i-e; imiddle; i+) t Leftri] - values[iJ: ntD right- new int[values. length-middle]: or Cint i-a; i<values.length-middle; i++) t rightli] -values [middle+i]; ort(left); //recursively...

  • in java Create an Eclipse Java project that utilize "Selection Sort" to sort an unsorted integer...

    in java Create an Eclipse Java project that utilize "Selection Sort" to sort an unsorted integer array of size 20 of random numbers in the range 0-500 inclusive. The main method should display the unsorted array and sorted array after Selection Sort.

  • Write a java function to process an array as follows: 1) reverse the contents of an...

    Write a java function to process an array as follows: 1) reverse the contents of an array 2) merge together two arrays 3) merge together two sorted arrays to yield a sorted array 4) append one array onto the end of another

  • 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,...

  • Given an unsorted array. The array has this property that every element in array is at...

    Given an unsorted array. The array has this property that every element in array is at most k distance from its position in sorted array where k is a positive integer smaller than size of array. Which sorting algorithm can be easily modified for sorting this array and what is the obtainable time complexity? Group of answer choices nsertion Sort with time complexity O(kn) Heap Sort with time complexity O(nLogk) Quick Sort with time complexity O(kLogk) Merge Sort with time...

  • 1.) Complete the merge method below, which is the merge step of MergeSort. That is, it...

    1.) Complete the merge method below, which is the merge step of MergeSort. That is, it takes two sorted arrays as input and returns a new sorted array that contains all the elements from the two input arrays. Furthermore, it should keep all duplicated values, and it should run in O(n) time, where n is the size of the output array. public static int[] main(int[] a, int[] b) { }

  • Order Statistics: A. Given two sorted arrays X and Y of equal size n, use Quick-Select...

    Order Statistics: A. Given two sorted arrays X and Y of equal size n, use Quick-Select to find the median of all 2n elements in arrays X and Y. Can you improve Quick-Select by choosing better pivots at every step?(in java) B. Show some experiments of your improved Quick-Select with arrays of size n=50. You can assume, if you want, that all the elements in the two arrays are distinct.

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

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
ADVERTISEMENT