FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_program/array/sorting/MergeSort.java at main · anuragkmr45/java_program · GitHub
anuragkmr45
/
java_program
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java_program
/
array
/
sorting
/
MergeSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (40 loc) · 1.15 KB
Breadcrumbs
java_program
/
array
/
sorting
/
MergeSort.java
Copy path
File metadata and controls
52 lines (40 loc) · 1.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
array
.
sorting
;
import
java
.
util
.
Arrays
;
public
class
MergeSort
{
static
int
[]
mergeSort
(
int
[]
array
){
if
(
array
.
length
==
1
) {
return
array
;
}
int
mid
=
array
.
length
/
2
;
int
[]
left
=
mergeSort
(
Arrays
.
copyOfRange
(
array
,
0
,
mid
));
int
[]
right
=
mergeSort
(
Arrays
.
copyOfRange
(
array
,
mid
,
array
.
length
));
return
merge
(
left
,
right
);
}
static
int
[]
merge
(
int
[]
first
,
int
[]
second
) {
int
[]
mix
=
new
int
[
first
.
length
+
second
.
length
];
int
i
=
0
;
int
j
=
0
;
int
k
=
0
;
while
(
i
<
first
.
length
&&
j
<
second
.
length
) {
if
(
first
[
i
] <
second
[
j
]) {
mix
[
k
] =
first
[
i
];
i
++;
}
else
{
mix
[
k
] =
second
[
j
];
}
k
++;
}
// it may be possible that array is not complites then copy yht remaining array
while
(
i
<
first
.
length
) {
mix
[
k
] =
first
[
i
];
i
++;
k
++;
}
while
(
j
<
second
.
length
) {
mix
[
k
] =
second
[
j
];
j
++;
k
++;
}
return
mix
;
}
}
Back
|
FazBrowse Home
|
New Git URL