-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathMergeSort.java
50 lines (44 loc) · 1.04 KB
/
MergeSort.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
42
43
44
45
46
47
48
49
50
public class MergeTwoSortedArrays {
public static void printArray(int[]arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static int[] merge2SortedArrays(int arr1[],int arr2[]) {
int m = arr1.length;
int n = arr2.length;
int arr[] = new int[m+n];
int i = 0;
int j = 0;
int k = 0;
while(i < m && j < n) { // this while loop is for comparing element between two array
if(arr1[i] <= arr2[j]) {
arr[k] = arr1[i]; // array1 element
i++;
k++;
}
else {
arr[k] = arr2[j]; // array2 element
j++;
k++;
}
}
while(i < m) { // this while loop is for copying remaining data from the array1(if any left)
arr[k] = arr1[i];
i++;
k++;
}
while(j < n) { //this while loop is for copying remaining data from the array2(if any left)
arr[k] = arr2[j];
j++;
k++;
}
return arr;
}
public static void main(String[] args) {
int arr1[] = {1,4,6,10,13};
int arr2[] = {2,5,7,9};
int arr3[] = merge2SortedArrays(arr1,arr2);
printArray(arr3);
}
}