#include <stdio.h> |
#include <stdlib.h> |
/*首先根据个位数的数值,在走访数值时将它们分配至编号0到9的桶子中; |
接下来将这些桶子中的数值重新串接起来; |
接着再进行一次分配,这次是根据十位数来分配; |
接下来将这些桶子中的数值重新串接起来。 |
*/ |
int main ( void ) |
{ |
int data[10] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81}; |
int temp[10][10] = {0}; |
int order[10] = {0}; |
int i, j, k, n, lsd; |
k = 0; |
n = 1; |
printf ( "\n排序前: " ); |
for ( i = 0; i < 10; i++ ) |
printf ( "%d " , data[i] ); |
putchar ( '\n' ); |
while ( n <= 10 ) |
{ |
for ( i = 0; i < 10; i++ ) |
{ |
lsd = ( ( data[i] / n ) % 10 ); |
temp[lsd][order[lsd]] = data[i]; |
order[lsd]++; |
} |
printf ( "\n重新排列: " ); |
for ( i = 0; i < 10; i++ ) |
{ |
if ( order[i] != 0 ) |
for ( j = 0; j < order[i]; j++ ) |
{ |
data[k] = temp[i][j]; |
printf ( "%d " , data[k] ); |
k++; |
} |
order[i] = 0; |
} |
n *= 10; |
k = 0; |
} |
putchar ( '\n' ); |
printf ( "\n排序后: " ); |
for ( i = 0; i < 10; i++ ) |
printf ( "%d " , data[i] ); |
return 0; |
} |