-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting.java
More file actions
32 lines (31 loc) · 787 Bytes
/
Counting.java
File metadata and controls
32 lines (31 loc) · 787 Bytes
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
public class Counting{
public static void countSort(int arr[]){
int largest = Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
largest= Math.max(largest,arr[i]);
}
int count[] =new int[largest+1];
for(int i=0;i<arr.length;i++){
count[arr[i]]++;
}
int j=0;
for(int i=0;i<count.length;i++){
while(count[i]>0){
arr[j]=i;
j++;
count[i]--;
}
}
}
public static void printArr(int arr[]){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
int arr[]={4,6,2,7,3};
countSort(arr);
printArr(arr);
}
}