aha-algorithm/chapter1/d2.c

30 lines
898 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 简化桶排序
// 啊哈算法 P6
#include <stdio.h>
int main()
{
int book[1001], i, j, t, n;
for (i = 0; i <= 1000; i++)
book[i] = 0;
printf("请输入要排序的数的个数: ");
scanf("%d", &n); // 输入一个数 n表示接下来有 n 个数
printf("请输入 %d 个数,使用空格隔开: \n", n);
for (i = 1; i <= n; i++) // 循环读入 n 个数,并进行桶排序
{
scanf("%d", &t); // 把每一个数读入到变量 t 中
book[t]++; // 进行计数,对编号为 t 的桶放一个小旗子
}
for (i = 1000; i >= 0; i--) // 依次判断编号 1000~0 的桶
for (j = 1; j <= book[i]; j++) // 出现了几次就打印几次
printf("%d ", i); // 打印编号
// 用来暂停程序,以便查看程序输出的内容
// getchar();
// getchar();
return 0;
}