-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
68 lines (58 loc) · 1.24 KB
/
MergeSort.java
File metadata and controls
68 lines (58 loc) · 1.24 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
63
64
65
66
67
68
public class solution {
public static void mergeSort(int[] input){
if(input.length<=1)
{
return;
}
int b[] = new int[input.length/2];
int c[] = new int[input.length-b.length];
for(int i = 0;i<input.length/2;i++)
{
b[i]=input[i];
}
for(int i = input.length/2;i<input.length;i++)
{
c[i-input.length/2]=input[i];
}
mergeSort(b);
mergeSort(c);
merge(b,c,input);
}
public static void merge(int s1[],int s2[], int d[])
{
int i=0,j=0,k=0;
while(i<s1.length && j<s2.length)
{
if(s1[i]<=s2[j])
{
d[k]=s1[i];
i++;
k++;
}
else
{
d[k]=s2[j];
j++;
k++;
}
}
if(i<s1.length)
{
while(i<s1.length)
{
d[k]=s1[i];
i++;
k++;
}
}
if(j<s2.length)
{
while(j<s2.length)
{
d[k]=s2[j];
j++;
k++;
}
}
}
}