You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
10. 基 数 排 序
10.1 描 述
基 数 排 序 是 按 照 低 位 先 排 序 , 然 后 收 集 ; 再 按 照 高 位 排 序 , 然 后 再 收 集 ; 依 次 类 推 , 直 到 最 高 位 。
有 时 候 有 些 属 性 是 有 优 先 级 顺 序 的 , 先 按 低 优 先 级 排 序 , 再 按 高 优 先 级 排 序 。 最 后 的 次 序 就 是 高
优 先 级 高 的 在 前 , 高 优 先 级 相 同 的 低 优 先 级 高 的 在 前 。
10.2 复 杂 程 度
时 间 复 杂 度 O ( n * k )
空 间 复 杂 度 O ( n + k )
10.3 代 码
# include <stdio.h>
# include <string.h>
# define N 14 //数组长度
# define D 14 //最大位数
int GetDigit ( int M , int i ) //取整数M的第i位数
{
while ( i > 1 )
{
M / = 10 ;
i - - ;
}
return M % 10 ;
}
void RadixSort ( int num [ ] , int len )
{
int i , j , k , l , digit ;
int allot [ 10 ] [ N ] ; //《分配数组》
memset ( allot , 0 , sizeof ( allot ) ) ; //初始化《分配数组》
for ( i = 1 ; i < = D ; i + + )
{
//分配相应位数的数据,并存入《分配数组》
for ( j = 0 ; j < len ; j + + )
{
digit = GetDigit ( num [ j ] , i ) ;
k = 0 ;
while ( allot [ digit ] [ k ] )
k + + ;
allot [ digit ] [ k ] = num [ j ] ;
}
//将《分配数组》的数据依次收集到原数组中
l = 0 ;
for ( j = 0 ; j < 10 ; j + + )
{
k = 0 ;
while ( allot [ j ] [ k ] > 0 )
{
num [ l + + ] = allot [ j ] [ k ] ;
k + + ;
}
}
//每次分配,收集后初始化《分配数组》,用于下一位数的分配和收集
memset ( allot , 0 , sizeof ( allot ) ) ;
}
}
int main ( )
{
int num [ N ] = { 22 , 34 , 3 , 32 , 82 , 55 , 89 , 50 , 37 , 5 , 64 , 35 , 9 , 70 } ;
RadixSort ( num , N ) ;
for ( int i = 0 ; i < N ; i + + )
printf ( " %d " , num [ i ] ) ;
printf ( " \n " ) ;
return 0 ;
}
10.4 运 行 时 间
0.02213 s