-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.java
More file actions
49 lines (43 loc) · 960 Bytes
/
Copy pathCountSort.java
File metadata and controls
49 lines (43 loc) · 960 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class CountSort {
public static void main(String[] args) {
int a[]={2,1,1,0,2,5,4,0,2,8,7,7,9,2,0,1,9};
a=CountSort(a);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
static int[] CountSort(int a[])
{
int n=a.length;
int max=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
int k=max;
int count[]=new int[k+1];
System.out.println(k+ " is max");
for(int i=0;i<n;i++)
{
count[a[i]]++;
}
for(int i=1;i<k+1;i++)
{
count[i]=count[i]+count[i-1];
}
int b[]=new int[n];
for(int i=n-1;i>=0;i--)
{
b[--count[a[i]]]=a[i];
}
for(int i=0;i<n;i++)
{
a[i]=b[i];
}
return a;
}
}