I suppose you are not particular about merge sorting. You just need to make a third array and combine the result into 3rd array and sort it.
I that case just make a new array like
int[] num3 = new int[num1.length + num2.length];
System.arraycopy(num1, 0, num3, 0, num1.length);
System.arraycopy(num2, 0, num3, num1.length, num2.length);
and sort num3 with any sorting algorithm.
But if you are particular about merge sort
Merge sort works as follows
- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
- Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.
Final program would be like
import java.util.Arrays;
public class SortingDemo {
public static void main(String[] args) {
int[] num1 = new int[] { 5, 6, 2, 8 };
int[] num2 = new int[] { 1, 4, 10, 7 };
// Combine both into thrid array
int[] num3 = new int[num1.length + num2.length];
System.arraycopy(num1, 0, num3, 0, num1.length);
System.arraycopy(num2, 0, num3, num1.length, num2.length);
// Sort third array
mergeSort(num3);
System.out.println(Arrays.toString(num3));
// Largest number of two array
System.out.println("Largest : " + num3[num3.length - 1]);
}
public static int[] mergeSort(int[] arrayData) {
if (arrayData.length <= 1) {
return arrayData;
}
int[] first = new int[arrayData.length / 2];
int[] second = new int[arrayData.length - first.length];
System.arraycopy(arrayData, 0, first, 0, first.length);
System.arraycopy(arrayData, first.length, second, 0, second.length);
mergeSort(first);
mergeSort(second);
merge(first, second, arrayData);
return arrayData;
}
private static void merge(int[] first, int[] second, int[] result) {
int iFirst = 0;
int iSecond = 0;
int j = 0;
while (iFirst < first.length && iSecond < second.length) {
if (first[iFirst] < second[iSecond]) {
result[j] = first[iFirst];
iFirst++;
} else {
result[j] = second[iSecond];
iSecond++;
}
j++;
}
System.arraycopy(first, iFirst, result, j, first.length - iFirst);
System.arraycopy(second, iSecond, result, j, second.length - iSecond);
}
}
Explanation as per comment.
You have two arrays of 4 elements each. { 5, 6, 2, 8 }
and { 1, 4, 10, 7 }
. The requirement is to create a new array combining these arrays and sort the newly created array it. So not first ne declare a new array. The size of it will be the length of array 1 + length of array 2. So we create array
int[] num3 = new int[num1.length + num2.length];
Now we need to copy values from both arrays to new array. So usual practice is to use System.arraycopy method. The arguments lists are (source array, initial position to copy from source, target array, position to copy on target, total number of elements to copy). So we'll do like
System.arraycopy(num1, 0, num3, 0, num1.length);
So first it copies from num1 starting at 0th index to the new array, num3, strating from 0th index and all elements from num1. Its equvalent to writing num3[0] = num1[0]; num3[1] = num1[1]; etc up to length of num1 like num3[3] = num1[3]. (i.e length is for so index 0 to 4).
System.arraycopy(num2, 0, num3, num1.length, num2.length);
So second time when we copy we already have values in num3 until 3rd index. So we need to start at 4th index i.e. num3[4] = num2[0]. This is why we set the 3rd argument to num1.length. Rest is similar to first copy.
So now we have a new array with combined values from num1 and num2. Second requirement is to sort it. If you dont want to write any special sorting algorithm you could just use standard sort provided by Arrays class. Like
Arrays.sort(num3);
This will sort the new combined array. Alternatively we could wirte your own sorting. Then we could do any kind of sorting. Say bubble sort or merge sort or quick sort or anything. You can learn about all these algorithms from http://www.sorting-algorithms.com/ or with examples from http://thilinasameera.wordpress.com/2011/06/01/sorting-algorithms-sample-codes-on-java-c-and-matlab/
Since the comments were about merge sorting i just chose merge sort. The idea or merge sort is split the array into two sorted array and then merge this into a new array in sorted order. So in this case first we split the num3 in to half. Then split each into half again and again until the size of array is 1 via a recursive call. This is done in the first part of method mergeSort()
This means {5,6,2,8,1,4,10,7}
will now be come {5},{6},{2},{8},{1},{4},{10},{7}
. The idea here is if an array has only 1 element then that can be considered sorted. So now we have 8 arrays. Now we need to merge these in a sorted fashion taking 2 array at a time. This is what merge()
method does.
So first i'll take {5} and {6} and merge to one in sorted way {5,6}. then will merge {2} and {8} to {2,8} and so on to make 4 arrays with 2 elements each. {5,6}, {2,8}, {1,4}, {7,10}. Now repeate the process again with this 4 arrays and so on until we get one array. And that will be sorted.
Hope this helps. Please ask if you have more questions.