FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaArithmetic/src/sort/RadixSort.java at master · git-jhy/JavaArithmetic · GitHub
git-jhy
JavaArithmetic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaArithmetic
/
src
/
sort
/
RadixSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (58 loc) · 2.06 KB
Breadcrumbs
JavaArithmetic
/
src
/
sort
/
RadixSort.java
Copy path
File metadata and controls
85 lines (58 loc) · 2.06 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
package
sort
;
/**
* Created by zxc on 2018/3/23.
*/
public
class
RadixSort
{
public
static
void
main
(
String
[]
args
) {
int
[]
arrays
= {
6
,
4322
,
432
,
344
,
55
,
234
,
45
,
243
,
5
,
2
,
4
,
5
,
6
,
7
,
3245
,
345
,
345
,
234
,
68
,
65
};
radixSort
(
arrays
);
System
.
out
.
println
(
"公众号:Java3y"
+
arrays
);
}
public
static
void
radixSort
(
int
[]
arrays
) {
int
max
=
findMax
(
arrays
,
0
,
arrays
.
length
-
1
);
//需要遍历的次数由数组最大值的位数来决定
for
(
int
i
=
1
;
max
/
i
>
0
;
i
=
i
*
10
) {
int
[][]
buckets
=
new
int
[
arrays
.
length
][
10
];
//获取每一位数字(个、十、百、千位...分配到桶子里)
for
(
int
j
=
0
;
j
<
arrays
.
length
;
j
++) {
int
num
= (
arrays
[
j
] /
i
) %
10
;
//将其放入桶子里
buckets
[
j
][
num
] =
arrays
[
j
];
}
//回收桶子里的元素
int
k
=
0
;
//有10个桶子
for
(
int
j
=
0
;
j
<
10
;
j
++) {
//对每个桶子里的元素进行回收
for
(
int
l
=
0
;
l
<
arrays
.
length
;
l
++) {
//如果桶子里面有元素就回收(数据初始化会为0)
if
(
buckets
[
l
][
j
] !=
0
) {
arrays
[
k
++] =
buckets
[
l
][
j
];
}
}
}
}
}
/**
* 递归,找出数组最大的值
*
* @param arrays 数组
* @param L 左边界,第一个数
* @param R 右边界,数组的长度
* @return
*/
public
static
int
findMax
(
int
[]
arrays
,
int
L
,
int
R
) {
//如果该数组只有一个数,那么最大的就是该数组第一个值了
if
(
L
==
R
) {
return
arrays
[
L
];
}
else
{
int
a
=
arrays
[
L
];
int
b
=
findMax
(
arrays
,
L
+
1
,
R
);
//找出整体的最大值
if
(
a
>
b
) {
return
a
;
}
else
{
return
b
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL