forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmergeArrays.java
41 lines (36 loc) · 1.08 KB
/
mergeArrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Arrays;
public class mergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 3, 4, 6, 7, 9, 10}; //m=7 length
int[] arr2 = {2, 5, 8, 11}; //n=4 length
merge(arr1, 7, arr2, 4);
}
public static void merge(int[] nums1, int m, int[] nums2, int n) {
int[] nums = new int[m + n];
int j = 0;
for (int i = 0; i < nums.length; i++)
{
if (i > m - 1)
{
nums[i] = nums2[j];
j = +1;
} else {
nums[i] = nums1[i];
}
}
System.out.println(Arrays.toString(nums));
int temp;
for(int k=0;k<nums.length;k++)
{
for(int z=1;z<nums.length-k;z++)
{
if(nums[z]<nums[z-1]) {
temp = nums[z];
nums[z] = nums[z-1];
nums[z-1] = temp;
}
}
}
System.out.println(Arrays.toString(nums));
}
}