5#define BEAD(i, j) beads[i * max + j]
8void beadSort(
int *a,
int len) {
11 for (
int i = 1; i < len; i++)
16 unsigned char *beads =
new unsigned char[max * len];
17 memset(beads, 0,
static_cast<size_t>(max) * len);
20 for (
int i = 0; i < len; i++)
21 for (
int j = 0; j < a[i]; j++) BEAD(i, j) = 1;
23 for (
int j = 0; j < max; j++) {
26 for (
int i = 0; i < len; i++) {
32 for (
int i = len - sum; i < len; i++) BEAD(i, j) = 1;
36 for (
int i = 0; i < len; i++) {
38 for (j = 0; j < max && BEAD(i, j); j++) {
48 int a[] = {5, 3, 1, 7, 4, 1, 1, 20};
49 int len =
sizeof(a) /
sizeof(a[0]);
53 for (
int i = 0; i < len; i++) printf(
"%d ", a[i]);
T sum(const std::vector< std::valarray< T > > &A)