62,612
社区成员
发帖
与我相关
我的任务
分享
COUNTING-SORT(A, B, k)
1 for i ← 0 to k
2 do C[i] ← 0
3 for j ← 1 to length[A]
4 do C[A[j]] ← C[A[j]] + 1
5 ▹ C[i] now contains the number of elements equal to i.
6 for i ← 1 to k
7 do C[i] ← C[i] + C[i - 1]
8 ▹ C[i] now contains the number of elements less than or equal to i.
9 for j ← length[A] downto 1
10 do B[C[A[j]]] ← A[j]
11 C[A[j]] ← C[A[j]] - 1
public class CountingSort {
public static int[] countingSort(int[] A, int k) {
int[] B = new int[A.length];
int[] C = new int[k];
for (int i = 0; i < k; i++)
C[i] = 0;
for (int i = 0; i < A.length; i++)
C[A[i]] += 1;
for (int i = 1; i < k; i++)
C[i] += C[i - 1];
for (int i = A.length - 1; i >= 0; i--) {
B[C[A[i]] - 1] = A[i];
C[A[i]] -= 1;
}
return B;
}
public static void main(String[] args) {
int[] A = { 2, 5, 3, 0, 2, 3, 0, 3 };
int[] B = countingSort(A, 6);
for (int b : B) {
System.out.print(b + " ");
}
System.out.println();
}
}
RADIX-SORT(A, d)
1 for i ← 1 to d
2 do use a stable sort to sort array A on digit i
public class RadixSort {
private static int[] countingSort(int[] A, int d) {
int[] B = new int[A.length];
int[] C = new int[10];
for (int i = 0; i < 10; i++)
C[i] = 0;
for (int i = 0; i < A.length; i++)
C[A[i] % ((int) Math.pow(10, d + 1)) / ((int) Math.pow(10, d))] += 1;
for (int i = 1; i < 10; i++)
C[i] += C[i - 1];
for (int i = A.length - 1; i >= 0; i--) {
B[C[A[i] % ((int) Math.pow(10, d + 1)) / ((int) Math.pow(10, d))] - 1] = A[i];
C[A[i] % ((int) Math.pow(10, d + 1)) / ((int) Math.pow(10, d))] -= 1;
}
return B;
}
public static int[] radixSort(int[] A, int d) {
int[] B = new int[A.length];
for (int i = 0; i < A.length; i++)
B[i] = A[i];
for (int i = 0; i < d; i++) {
B = countingSort(B, i);
}
return B;
}
public static void main(String[] args) {
int[] A = { 12, 25, 13, 30, 32, 43, 20, 3 };
int[] B = radixSort(A, 2);
for (int b : B) {
System.out.print(b + " ");
}
System.out.println();
}
}
BUCKET-SORT(A)
1 n ← length[A]
2 for i ← 1 to n
3 do insert A[i] into list B[⌊n A[i]⌋]
4 for i ← 0 to n - 1
5 do sort list B[i] with insertion sort
6 concatenate the lists B[0], B[1], . . ., B[n - 1] together in order
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class BucketSort {
public static <T> void insertionSort(T[] t, Comparator<? super T> comparator) {
for (int j = 1; j < t.length; j++) {
T key = t[j];
int i = j - 1;
while (i > -1 && comparator.compare(t[i], key) > 0) {
t[i + 1] = t[i];
i--;
}
t[i + 1] = key;
}
}
public static Double[] bucketSort(Double[] A) {
List<Double>[] B = new List[10];
for (int i = 0; i < B.length; i++)
B[i] = new LinkedList<Double>();
for (Double d : A)
B[(int) (d * 10)].add(d);
Double[][] BB = new Double[10][];
List<Double> BBB = new LinkedList<Double>();
for (int i = 0; i < B.length; i++) {
BB[i] = B[i].toArray(new Double[0]);
insertionSort(BB[i], new Comparator<Double>() {
public int compare(Double o1, Double o2) {
return (int) (Math.signum(o1 - o2));
}
});
for (Double d : BB[i])
BBB.add(d);
}
return BBB.toArray(new Double[0]);
}
public static void main(String[] args) {
Double[] doubles = new Double[] { 0.78, 0.17, 0.39, 0.26, 0.72, 0.94,
0.21, 0.12, 0.23, 0.68 };
doubles = bucketSort(doubles);
for (Double d : doubles)
System.out.print(d + " ");
System.out.println();
}
}