标签:class c t sp strong string int re new for id
package algorithm.sort;
public class CountingSort {
public static void main(String[] args) {
int[] a = new int[] {5, 3, 7, 2, 1, 4, 9, 8, 6, 1};
int[] b = sort(a, 9);
print(b);
}
public static int[] sort(int[] a, int k) {
if (a == null || a.length < 2)
return a;
int[] c = new int[k + 1];
for (int i = 0; i < a.length; i++) {
c[a[i]] = c[a[i]] + 1;
}
for (int i = 1; i < c.length; i++) {
c[i] = c[i] + c[i - 1];
}
int[] b = new int[a.length];
for (int i = a.length - 1; i >= 0; i--) {
b[c[a[i]] - 1] = a[i];
c[a[i]] = c[a[i]] - 1;
}
return b;
}
public static void exchange(int[] a, int i1, int i2) {
int tmp = a[i1];
a[i1] = a[i2];
a[i2] = tmp;
}
public static void print(int[] a) {
if (a == null || a.length == 0)
return;
for (int i = 1; i <= a.length; i++) {
System.out.print(a[i - 1] + "\t");
if (i % 5 == 0)
System.out.println();
}
}
}
标签:class c t sp strong string int re new for id
原文地址:http://www.cnblogs.com/look4liming/p/3702230.html