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
  • 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){

  • Write a java method merge that accepts two arrays of integers and returns a new array...

    Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10};   int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]

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

  • Create an Eclipse Java project that utilize "Bubble Sort" to sort an unsorted integer array of...

    Create an Eclipse Java project that utilize "Bubble Sort" to sort an unsorted integer array of size 10. The main method should display unsorted array and sorted array after Bubble Sort. NEED A WORKING JAVA PROJECT. NEED IT ASAP

  • I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...

    I need help with this .java implementation: Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time. The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted. Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays...

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

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