public class CountingSort
{
public static void CountingSort(int[] a, int[] b, int k)
{
int[] c = new int[k];
for (int i = 0; i < k; ++i)
{
c[i] = 0;
}
for (int j = 0; j < a.length; ++j)
{
c[a[j]] = c[a[j]] + 1;
}
for (int i = 1; i < k; ++i)
{
c[i] = c[i] + c[i-1];
}
for (int j = 0; j < a.length; ++j)
{
b[c[a[j]]-1] = a[j];
c[a[j]] = c[a[j]] - 1;
}
}
public static void main(String[] args)
{
int[] a = {2, 5, 3, 0, 2, 3, 0, 3};
int[] b = new int[a.length];
CountingSort(a, b, 6);
for (int tmp : b)
{
System.out.println(tmp);
}
}
}
{
public static void CountingSort(int[] a, int[] b, int k)
{
int[] c = new int[k];
for (int i = 0; i < k; ++i)
{
c[i] = 0;
}
for (int j = 0; j < a.length; ++j)
{
c[a[j]] = c[a[j]] + 1;
}
for (int i = 1; i < k; ++i)
{
c[i] = c[i] + c[i-1];
}
for (int j = 0; j < a.length; ++j)
{
b[c[a[j]]-1] = a[j];
c[a[j]] = c[a[j]] - 1;
}
}
public static void main(String[] args)
{
int[] a = {2, 5, 3, 0, 2, 3, 0, 3};
int[] b = new int[a.length];
CountingSort(a, b, 6);
for (int tmp : b)
{
System.out.println(tmp);
}
}
}