-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergetwosortedarrays.java
More file actions
62 lines (50 loc) · 1.13 KB
/
mergetwosortedarrays.java
File metadata and controls
62 lines (50 loc) · 1.13 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
package July2;
public class mergetwosortedarrays {
public static int[] merge(int one[],int second[]) {
int i=0,j=0,k=0;
int sorted[]=new int[one.length+second.length];
while(i<one.length && j<second.length) {
if(one[i]<=second[j]) {
sorted[k]=one[i];
i++;
k++;
}else {
sorted[k]=second[j];
j++;
k++;
}
}
while(i<one.length) {
sorted[k]=one[i];
i++;k++;
}
while(j<second.length) {
sorted[k]=second[j];
j++;k++;
}
return sorted;
}
public static int[] mergesort(int arr[],int low,int high) {
if(low==high) {
int ba[]=new int[1];
ba[0]=arr[low];
return ba;
}
int mid=(low+high)/2;
int arr1[]=mergesort(arr,low,mid);
int arr2[]=mergesort(arr,mid+1,high);
arr=merge(arr1,arr2);
return arr;
}
public static void main(String[] args) {
//int one[]= {1,3,7,8,9};
//int second[]= {2,3,4,6,8,9};
//int sorted[]=merge(one,second);
int arr[]= {1,5,4,2,9,5,6,7};
arr=mergesort(arr,0,arr.length-1);
for(int val:arr) {
System.out.print(val+" ");
}
System.out.println();
}
}