# 【排序】--C语言实现基数排序

## 代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

{
int i, max = a[0], base = 1;;
for (i = 1; i < length; i++)
{
if (a[i] > max)
{
max = a[i];
}
}

int *t = (int *)malloc(sizeof(int) * length);
while (max / base > 0)
{
int bucket[10] = {0};

for (i = 0; i < length; i++)
{
bucket[a[i] / base % 10]++;
}
for (i = 1; i < 10; i++)
{
bucket[i] += bucket[i - 1];
}
for (i = length - 1; i >= 0; i--)
{
t[bucket[a[i] / base % 10] - 1] = a[i];  //原始序列中最后一个数据，放在新序列下标为 bucket[a[i] / base % 10] - 1 的地方。
bucket[a[i] / base % 10]--;
}
for (i = 0; i < length; i++)
{
a[i] = t[i];
}

base = base * 10;
}
}

int main()
{
int i;
//int array[10] = {9, 3, 4, 4, 6, 0, 1, 3, 8, 5};
int array[10000] = {0};
srand(time(NULL));
for (i = 0; i < 10000; i++)
{
array[i] = rand() % 1000;
}

for (i = 0; i < sizeof(array) / sizeof(array[0]); i++)
{
printf("%d ", array[i]);
}
printf("\n");

return 0;
}



12-03
01-20 1518

06-23 2297
06-08 391
10-09 1265
10-14 5830
06-04 759
08-14 2万+
10-24 1万+
01-17 192