1)判断数据在各位的大小,排列数据;
2)根据1的结果,判断数据在十分位的大小,排列数据。如果数据在这个位置的余数相同,那么数据之间的顺序根据上一轮的排列顺序确定;
3)依次类推,继续判断数据在百分位、千分位......上面的数据重新排序,直到所有的数据在某一分位上数据都为0。
#include <stdio.h>
#include <assert.h>
#include
<malloc.h>
#include <string.h>
int pre_process_data(int
array[], int length, int weight)
{
int
index;
int value = 1;
assert(NULL !=
array && 0 != length);
for(index = 0; index <
weight; index ++)
value *=
10;
for(index = 0; index < length; index
++)
{
array[index] = (array[index]%value)/(value/10);
}
for(index = 0; index < length; index
++)
{
if(0
!=
array[index])
return 1;
}
return
0;
}
void sort_for_basic_value(int array[], int pData[], int length,
int swap[])
{
int value;
int
index;
int total = 0;
for(value =
-9; value < 10; value ++)
{
for(index = 0; index <
length; index ++)
{
if(-10
!= pData[index] && pData[index] ==
value)
{
swap[total++] =
array[index];
pData[index] =
-10;
}
}
}
memmove(array, swap, sizeof(int) *
length);
}
void basic_sort(int array[], int
length)
{
int weight = 1;
int
*pData = malloc(sizeof(int) * length);
memmove(pData,
array, sizeof(int) * length);
int *swap =
malloc(sizeof(int) * length);
while(1)
{
if(!pre_process_data(pData, length,
weight))
break;
else
{
weight
++;
}
sort_for_basic_value(array,
pData, length, swap);
memmove(pData, array, sizeof(int) * length);
}
}
void test()
{
int
i;
int array[10] =
{1,3,14,5,8,6,7,9,0,-2};
basic_sort(array,
10);
for(i = 0; i < 10; i ++)
{
printf("%d, ",
array[i]);
}
printf("\n");
}
int main()
{
test();
}
原文:http://www.cnblogs.com/chengxuyuandashu/p/3573212.html