forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSort.java
More file actions
23 lines (22 loc) · 603 Bytes
/
Copy pathcountSort.java
File metadata and controls
23 lines (22 loc) · 603 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class countSort {
public static void main(String[] args) {
int[] arr = {5,4,3,3,2,1}; //created array
int k = 5;
int n = arr.length;
int[] countArr = new int[k+1];
for(int i=1;i<=k;i++) countArr[i] = 0;
for(int i=0;i<n;i++){
countArr[arr[i]]++;
}
int i = 0,idx=0;
while(i <= k){
if(countArr[i] == 0) i++;
else{
arr[idx] = i;
countArr[i]--;
idx++;
}
}
for(i=0;i<n;i++) System.out.print(arr[i] +" ");
}
}