# 分配类排序——基数排序

640人阅读 评论(0)

基数排序属于“分配式排序”（distribution sort），基数排序法又称“桶子法”（bucket sort）或bin sort，顾名思义，它是透过键值的部份资讯，将要排序的元素分配至某些“桶”中，藉以达到排序的作用，基数排序法是属于稳定性的排序

#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define KEY_SIZE 6                    //键的最大个数
#define LIST_SIZE 20                  //需要排序数的个数
typedef struct
{
int key;
int next;                          //静态链域
} RecordType1;

typedef struct
{
RecordType1 r[LIST_SIZE + 1];
int length;
int keynum;

int num[LIST_SIZE][KEY_SIZE];            //将整数进行分离

/*将输入的十进制数的每一位拆分开来*/
{
int i, m, j, tmp;
int r_num;

m = s_num->keynum;
for (i = 1; i <= s_num->length; i++)
{
r_num = s_num->r[i].key;
for (j = m-1; j >= 0; j--)
{
tmp = r_num % 10;
num[i][j] = tmp;
r_num = r_num / 10;
while (r_num == 0 && j != 0)
{
j--;
num[i][j] = 0;
break;
}
}
}
}

/*分配*/
{
int j, p;
for (j = 0; j <= RADIX-1; ++j)
{
}
p = s_num->r[0].next;
while(p != 0)
{
j = num[p][i];
else
s_num->r[tail[j]].next = p;
tail[j] = p;
p = s_num->r[p].next;
}
}

/*回收*/
{
int j, t;
j = 0;
++j;
t = tail[j];
while (j < RADIX - 1)
{
++j;
{
++j;
}
{
t = tail[j];
}
}
s_num->r[t].next = 0;
}

//找最大数
{
int i, max = 0;
for (i = 1; i <= s_num->length; i++)
{
if (s_num->r[i].key > max)
max = s_num->r[i].key;
}
return max;
}

/*确定关键字的个数(找最大数关键子的位数)*/
{
int max_num, temp = 0, count = 1;

max_num = maxNum(s_num);                  //找最大数

temp = max_num / 10;
while (temp != 0)
{
count++;
temp = temp / 10;
}
s_num->keynum = count;
}

/*基数排序*/
{
int i, d, n;

n = s_num->length;
for (i = 0; i <= n-1; ++i)
s_num->r[i].next = i+1;
s_num->r[n].next = 0;

d = s_num->keynum;                          //关键字的个数
for (i = d-1; i >= 0; i--)
{
}
}

/*输出排序后的结果*/
{
int j;
j = s_num->r[0].next;

printf("\nsort finish:");
while (j != 0)
{
printf("%d  ", s_num->r[j].key);
j = s_num->r[j].next;
}
printf("\n");
}

int main(int argc, char *argv[])
{
int i;

printf("input sort count is : ");
scanf("%d", &s_num->length);

printf("\nPlease input sort number is :");
for (i = 1; i <= s_num->length; i++)
{
scanf("%d", &s_num->r[i].key);
}

Keynum(s_num);             //计算关键字的个数
Order(s_num);              //将输入的十进制数进行拆分
Print(s_num);

return EXIT_SUCCESS;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：51399次
• 积分：1162
• 等级：
• 排名：千里之外
• 原创：63篇
• 转载：7篇
• 译文：0篇
• 评论：20条
评论排行
最新评论