FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/sorting/MergeSortNonRecursive.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
/
sorting
/
MergeSortNonRecursive.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (48 loc) · 948 Bytes
Breadcrumbs
java
/
sorting
/
MergeSortNonRecursive.java
Copy path
File metadata and controls
56 lines (48 loc) · 948 Bytes
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
public
class
NonRecursiveMergeSort
<
T
extends
Comparable
<
T
>> {
public
T
[]
MergeSort
(
T
[]
M
) {
int
length
=
M
.
length
;
int
size
=
1
;
int
p
;
while
(
size
<
length
) {
p
= -
1
;
while
(
p
+
size
<
length
) {
int
left
=
p
+
1
;
int
right
=
left
+
size
-
1
;
if
(
right
+
size
<=
length
) {
p
=
right
+
size
;
}
else
{
p
=
length
;
}
merge
(
M
,
left
,
right
,
p
);
}
size
=
size
*
2
;
}
return
M
;
}
public
T
[]
merge
(
T
[]
M
,
int
left
,
int
right
,
int
p
) {
int
i
=
left
;
int
j
=
right
+
1
;
int
k
=
left
;
T
[]
M2
=
M
.
clone
();
for
(
int
n
=
0
;
n
<
M
.
length
;
n
++) {
M2
[
n
] =
null
;
}
while
(
i
<=
right
&&
j
<=
p
) {
if
(
M
[
i
].
compareTo
(
M
[
j
]) <=
0
) {
M2
[
k
] =
M
[
i
];
i
=
i
+
1
;
}
else
{
M2
[
k
] =
M
[
j
];
j
=
j
+
1
;
}
k
=
k
+
1
;
}
for
(
int
h
=
i
;
h
<=
right
;
h
++) {
M
[
k
+ (
h
-
i
)] =
M
[
h
];
}
for
(
int
h
=
left
;
h
<= (
k
-
1
);
h
++) {
M
[
h
] =
M2
[
h
];
}
return
M
;
}
}
Back
|
FazBrowse Home
|
New Git URL