FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/src/main/java/com/examplehub/sorts/MergeSort.java at master · JavaCimcoz/Java · GitHub
JavaCimcoz
/
Java
Public
forked from
examplehub/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
Java
/
src
/
main
/
java
/
com
/
examplehub
/
sorts
/
MergeSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (93 loc) · 2.71 KB
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
examplehub
/
sorts
/
MergeSort.java
Copy path
File metadata and controls
102 lines (93 loc) · 2.71 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
94
95
96
97
98
99
100
101
102
package
com
.
examplehub
.
sorts
;
public
class
MergeSort
implements
Sort
{
@
Override
public
void
sort
(
int
[]
numbers
) {
mergeSort
(
numbers
,
0
,
numbers
.
length
-
1
);
}
@
Override
public
<
T
extends
Comparable
<
T
>>
void
sort
(
T
[]
array
) {
mergeSort
(
array
,
0
,
array
.
length
-
1
);
}
/**
* Merge sort algorithm implements.
*
* @param numbers the numbers to be sorted.
* @param left the left index of sub array.
* @param right the right index of sub array.
*/
public
static
void
mergeSort
(
int
[]
numbers
,
int
left
,
int
right
) {
if
(
left
<
right
) {
int
middle
= (
left
+
right
) >>
1
;
mergeSort
(
numbers
,
left
,
middle
);
mergeSort
(
numbers
,
middle
+
1
,
right
);
merge
(
numbers
,
left
,
middle
,
right
);
}
}
/**
* Generic MergeSort algorithm implements.
*
* @param array the array to be sorted.
* @param left the left index of sub array.
* @param right the right index of sub array.
* @param <T> the class of the objects in the array.
*/
public
static
<
T
extends
Comparable
<
T
>>
void
mergeSort
(
T
[]
array
,
int
left
,
int
right
) {
if
(
left
<
right
) {
int
middle
= (
left
+
right
) >>
1
;
mergeSort
(
array
,
left
,
middle
);
mergeSort
(
array
,
middle
+
1
,
right
);
merge
(
array
,
left
,
middle
,
right
);
}
}
/**
* @param numbers the numbers to be merged.
* @param left the left index of first sub array.
* @param middle the right index of second sub array.
* @param right the right index of second sub array.
*/
public
static
void
merge
(
int
[]
numbers
,
int
left
,
int
middle
,
int
right
) {
int
i
=
left
;
int
j
=
middle
+
1
;
int
k
=
left
;
while
(
i
<=
middle
&&
j
<=
right
) {
if
(
numbers
[
i
] <=
numbers
[
j
]) {
numbers
[
k
++] =
numbers
[
i
++];
}
else
{
numbers
[
k
++] =
numbers
[
j
++];
}
}
while
(
i
<=
middle
) {
numbers
[
k
++] =
numbers
[
i
++];
}
while
(
j
<=
right
) {
numbers
[
k
++] =
numbers
[
j
++];
}
}
/**
* Generic merge two sub sorted numbers.
*
* @param array the array to be sorted.
* @param left the left index of first sub array.
* @param middle the right index of second sub array.
* @param right the right index of second sub array.
* @param <T> the class of the objects in the array.
*/
public
static
<
T
extends
Comparable
<
T
>>
void
merge
(
T
[]
array
,
int
left
,
int
middle
,
int
right
) {
int
i
=
left
;
int
j
=
middle
+
1
;
int
k
=
left
;
while
(
i
<=
middle
&&
j
<=
right
) {
if
(
array
[
i
].
compareTo
(
array
[
j
]) <=
0
) {
array
[
k
++] =
array
[
i
++];
}
else
{
array
[
k
++] =
array
[
j
++];
}
}
while
(
i
<=
middle
) {
array
[
k
++] =
array
[
i
++];
}
while
(
j
<=
right
) {
array
[
k
++] =
array
[
j
++];
}
}
}
Back
|
FazBrowse Home
|
New Git URL