代码如下:
#include <iostream>
#include <cmath>
using namespace std;
#define INF -100000
#define MAXSIZE 30
typedef int ElemType;
typedef struct Node
{
ElemType e;
struct Node *next;
} * List;
typedef struct BNode
{
List l[10];
} * Bucket;
List creatlist(int n=0)
{
List l = (List)malloc(sizeof(List));
l->next = NULL;
l->e = n;
return l;
}
Bucket creatbucket()
{
Bucket b = (Bucket)malloc(sizeof(struct BNode));
for (int i = 0; i < 10; i++)
{
b->l[i]=creatlist();
}
return b;
}
void MSD(ElemType A[], ElemType *tmpA, int st, int end, int MAXdigit)
{
for (int i = 0; i < MAXdigit; i++)
{
Bucket b = creatbucket();
for (int j = end; j >= st; --j)
{
int tmp = A[j] / pow(10, i);
List tl = creatlist(A[j]);
tl->next = b->l[tmp % 10]->next;
b->l[tmp % 10]->next = tl;
}
int k = st;
for (int j = st; j <= end; ++j)
{
List tmp = b->l[j]->next;
while (tmp)
{
A[k++] = tmp->e;
tmp = tmp->next;
}
}
// for (int i = 0; i <= end; i++)可以查看每一趟排序结果,改下变量名,复制粘贴到其他排序也可查看
// {
// cout << A[i] << " ";
// }
// cout << endl;
}
}
void MSDRadixSort(ElemType A[], int N)
{
ElemType *tmpA = (ElemType *)malloc(sizeof(ElemType) * N);
if (tmpA)
MSD(A, tmpA, 0, N - 1, 3);
else
exit(0);
}