FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/algorithms/sorting/MergeSort.java at master · AllAlgorithms/java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
java
Public archive
Notifications
You must be signed in to change notification settings
Fork
83
Star
116
Code
Issues
3
Pull requests
6
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
algorithms
/
sorting
/
MergeSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (72 loc) · 2.08 KB
Breadcrumbs
java
/
algorithms
/
sorting
/
MergeSort.java
Copy path
File metadata and controls
93 lines (72 loc) · 2.08 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Java implementation of merge sort
*
* @author Carlos Abraham Hernandez
* @email abraham@abranhe.com
*/
import
java
.
util
.
Arrays
;
public
class
MergeSort
{
// Merge the two half into a sorted data.
public
void
merge
(
int
arr
[],
int
left
,
int
middle
,
int
right
) {
int
n1
=
middle
-
left
+
1
;
int
n2
=
right
-
middle
;
int
L
[] =
new
int
[
n1
];
int
R
[] =
new
int
[
n2
];
for
(
int
i
=
0
;
i
<
n1
; ++
i
)
L
[
i
] =
arr
[
left
+
i
];
for
(
int
j
=
0
;
j
<
n2
; ++
j
)
R
[
j
] =
arr
[
middle
+
1
+
j
];
int
i
=
0
,
j
=
0
;
int
k
=
left
;
while
(
i
<
n1
&&
j
<
n2
) {
if
(
L
[
i
] <=
R
[
j
]) {
arr
[
k
] =
L
[
i
];
i
++;
}
else
{
arr
[
k
] =
R
[
j
];
j
++;
}
k
++;
}
while
(
i
<
n1
) {
arr
[
k
] =
L
[
i
];
i
++;
k
++;
}
/* Copy remaining elements of R[] if any */
while
(
j
<
n2
) {
arr
[
k
] =
R
[
j
];
j
++;
k
++;
}
}
// Main function that sorts arr[l..r] using
void
sort
(
int
arr
[],
int
left
,
int
right
) {
if
(
left
<
right
) {
// Find the middle point
int
middle
= (
left
+
right
) /
2
;
// Sort first and second halves
sort
(
arr
,
left
,
middle
);
sort
(
arr
,
middle
+
1
,
right
);
// Merge the sorted halves
merge
(
arr
,
left
,
middle
,
right
);
}
}
// Default values
void
sort
(
int
arr
[]) {
int
l
=
0
;
// First index
int
r
=
arr
.
length
-
1
;
// Last index
sort
(
arr
,
l
,
r
);
}
// Test Functionality
public
static
void
main
(
String
args
[]) {
int
[]
arr
=
new
int
[
20
];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++) {
arr
[
i
] = (
int
) (
Math
.
random
() *
999
+
1
);
}
System
.
out
.
println
(
"Unordered List:
\n
"
+
Arrays
.
toString
(
arr
));
MergeSort
m
=
new
MergeSort
();
m
.
sort
(
arr
);
System
.
out
.
println
(
"Ordered List:
\n
"
+
Arrays
.
toString
(
arr
));
}
}
Back
|
FazBrowse Home
|
New Git URL