FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-basics/java-programs/CountingSort.java at patch-1 · CodeForHunger/java-basics · GitHub
CodeForHunger
/
java-basics
Public
forked from
learning-zone/java-basics
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-basics
/
java-programs
/
CountingSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (33 loc) · 1.01 KB
Breadcrumbs
java-basics
/
java-programs
/
CountingSort.java
Copy path
File metadata and controls
34 lines (33 loc) · 1.01 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
package
misc
;
import
java
.
util
.
Arrays
;
/**
* Counting sort is used to sort elements between specific range.
* Counting sort calculates the number of occurrence of objects and stores its key values.
* New array is formed by adding previous key elements and assigning to objects.
* It can only sort an integer array or number array e.g. short, byte or char array.
*
*/
public
class
CountingSort
{
public
static
void
countingSort
(
int
[]
input
,
int
k
) {
int
counter
[] =
new
int
[
k
+
1
];
// fill bucket
for
(
int
i
:
input
) {
counter
[
i
]++;
}
// sort array
int
ndx
=
0
;
for
(
int
i
=
0
;
i
<
counter
.
length
;
i
++) {
while
(
0
<
counter
[
i
]) {
input
[
ndx
++] =
i
;
counter
[
i
]--;
}
}
}
public
static
void
main
(
String
[]
args
) {
int
[]
input
= {
60
,
40
,
30
,
20
,
10
,
40
,
30
,
60
,
60
,
20
,
40
,
30
,
40
};
int
k
=
60
;
System
.
out
.
println
(
"Integer array before sorting: "
+
Arrays
.
toString
(
input
));
countingSort
(
input
,
k
);
System
.
out
.
println
(
"Integer array after sorting: "
+
Arrays
.
toString
(
input
));
}
}
Back
|
FazBrowse Home
|
New Git URL