FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Sorts/BucketSort.java at master · rpcwork/Java · GitHub
rpcwork
/
Java
Public
forked from
TheAlgorithms/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
/
Sorts
/
BucketSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (55 loc) · 1.77 KB
Breadcrumbs
Java
/
Sorts
/
BucketSort.java
Copy path
File metadata and controls
72 lines (55 loc) · 1.77 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
package
Sorts
;
import
java
.
util
.
Random
;
public
class
BucketSort
{
static
int
[]
sort
(
int
[]
sequence
,
int
maxValue
)
{
// Bucket Sort
int
[]
Bucket
=
new
int
[
maxValue
+
1
];
int
[]
sorted_sequence
=
new
int
[
sequence
.
length
];
for
(
int
i
=
0
;
i
<
sequence
.
length
;
i
++)
Bucket
[
sequence
[
i
]]++;
int
outPos
=
0
;
for
(
int
i
=
0
;
i
<
Bucket
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
Bucket
[
i
];
j
++)
sorted_sequence
[
outPos
++] =
i
;
return
sorted_sequence
;
}
static
void
printSequence
(
int
[]
sorted_sequence
)
{
for
(
int
i
=
0
;
i
<
sorted_sequence
.
length
;
i
++)
System
.
out
.
print
(
sorted_sequence
[
i
] +
" "
);
}
static
int
maxValue
(
int
[]
sequence
)
{
int
maxValue
=
0
;
for
(
int
i
=
0
;
i
<
sequence
.
length
;
i
++)
if
(
sequence
[
i
] >
maxValue
)
maxValue
=
sequence
[
i
];
return
maxValue
;
}
public
static
void
main
(
String
args
[])
{
System
.
out
.
println
(
"Sorting of randomly generated numbers using BUCKET SORT"
);
Random
random
=
new
Random
();
int
N
=
20
;
int
[]
sequence
=
new
int
[
N
];
for
(
int
i
=
0
;
i
<
N
;
i
++)
sequence
[
i
] =
Math
.
abs
(
random
.
nextInt
(
100
));
int
maxValue
=
maxValue
(
sequence
);
System
.
out
.
println
(
"
\n
Original Sequence: "
);
printSequence
(
sequence
);
System
.
out
.
println
(
"
\n
Sorted Sequence: "
);
printSequence
(
sort
(
sequence
,
maxValue
));
}
}
/*
#Output:
$ javac Bucket_Sort.java
$ java Bucket_Sort
Sorting of randomly generated numbers using BUCKET SORT
Original Sequence:
95 9 95 87 8 81 18 54 57 53 92 15 38 24 8 56 29 69 64 66
Sorted Sequence:
8 8 9 15 18 24 29 38 53 54 56 57 64 66 69 81 87 92 95 95
*/
Back
|
FazBrowse Home
|
New Git URL