FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Algorithms/Sorting/MergeSort.java at master · nimit95/Data-Structures-Algorithms · GitHub
nimit95
/
Data-Structures-Algorithms
Public
forked from
CodersForLife/Data-Structures-Algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
MergeSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (49 loc) · 1.53 KB
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
MergeSort.java
Copy path
File metadata and controls
61 lines (49 loc) · 1.53 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
public
class
MergeSort
{
public
static
void
mergeSort
(
int
[]
arr
) {
mergeSort
(
arr
,
0
,
arr
.
length
-
1
);
}
public
static
void
mergeSort
(
int
[]
arr
,
int
leftIndex
,
int
rightIndex
) {
int
arrLen
=
rightIndex
+
1
-
leftIndex
;
if
(
arrLen
>
1
) {
int
midIndex
= (
leftIndex
+
rightIndex
) /
2
;
mergeSort
(
arr
,
leftIndex
,
midIndex
);
mergeSort
(
arr
,
midIndex
+
1
,
rightIndex
);
merge
(
arr
,
leftIndex
,
midIndex
,
rightIndex
);
}
}
public
static
void
merge
(
int
[]
arr
,
int
leftIndex
,
int
midIndex
,
int
rightIndex
) {
int
leftLen
=
midIndex
+
1
-
leftIndex
;
int
rightLen
=
rightIndex
-
midIndex
;
int
[]
leftArr
=
new
int
[
leftLen
];
int
[]
rightArr
=
new
int
[
rightLen
];
for
(
int
i
=
0
;
i
<
leftLen
;
i
++) {
leftArr
[
i
] =
arr
[
i
+
leftIndex
];
}
for
(
int
j
=
0
;
j
<
rightLen
;
j
++) {
rightArr
[
j
] =
arr
[
j
+
midIndex
+
1
];
}
int
i
=
0
;
int
j
=
0
;
int
k
=
leftIndex
;
while
(
i
<
leftLen
&&
j
<
rightLen
) {
if
(
leftArr
[
i
] <
rightArr
[
j
]) {
arr
[
k
] =
leftArr
[
i
];
i
++;
}
else
{
arr
[
k
] =
rightArr
[
j
];
j
++;
}
k
++;
}
while
(
i
<
leftLen
) {
arr
[
k
] =
leftArr
[
i
];
i
++;
k
++;
}
while
(
j
<
rightLen
) {
arr
[
k
] =
rightArr
[
j
];
j
++;
k
++;
}
}
}
Back
|
FazBrowse Home
|
New Git URL