博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基数排序
阅读量:6975 次
发布时间:2019-06-27

本文共 1999 字,大约阅读时间需要 6 分钟。

  hot3.png

算法把一个数字每一位当作一个基数进行排序,从最低位开始, 依次进行一次排序.这样从最低位排序一直到最高位排序完成以后, 数列就变成一个有序序列。每个基数的排序使用计数排序。

#include 
   #include 
     int max_bit(int a[], int size) /* get the max bit of number */  {       int i, max, b;         for (i = 1, max = a[0]; i < size; i++) {           if (a[i] > max) {               max = a[i];           }       }       b = 1;       while (max / 10) {           b++;           max /= 10;       }       return b;   }     void radix_sort(int a[], int size)   {       int d = max_bit(a, size);       int i, j, k, range, radix;       int *c, *b;         range = 10; /* for counting sort, the range of every bit is 0 - 9 */      radix = 1;       c = (int *)malloc(sizeof(int) * range);       b = (int *)malloc(sizeof(int) * size);       for (i = 0; i < d; i++, radix *= 10) {                      /* use counting sort */          /* clear count before every sort */          for (j = 0; j < range; j++) {                c[j] = 0;           }             /* c[k] content the num of elements which equal to k */          for (j = 0; j < size; j++) {                k = (a[j] / radix) % 10;               c[k]++;           }             /* c[j] content the num of elements which equal or less than j */          for (j = 1; j < range; j++)                c[j] += c[j-1];             /* put a[j] into the space of b[c[k] - 1] */          for (j = size - 1; j >= 0; j--) {                k = (a[j] / radix) % 10;               b[c[k] - 1] = a[j];               c[k]--;           }             /* copy the 'sorted' list to a[j] */          for (j = 0; j < size; j++)               a[j] = b[j];       }       free(c);       free(b);           }     main()   {       int a[] = {329, 457, 657, 839, 436, 720, 355}, i;       int size = sizeof(a) / sizeof(int);       radix_sort(a, size);              for (i = 0; i < size; i++)           printf("%d ", a[i]);   }

Reference

[1].http://blog.csdn.net/houapple/article/details/6480042

转载于:https://my.oschina.net/lvyi/blog/325610

你可能感兴趣的文章
最近发生的事情
查看>>
个人作业1-——数组
查看>>
xargs和exec详解
查看>>
Mybatis配置insert时,插入数据失败
查看>>
el表达式不起作用
查看>>
CLOSE_WAIT & TIME_WAIT
查看>>
nagios实现对linux-server、windows-server主机的监控
查看>>
Linux系统上的命令使用格式及部分命令详细介绍
查看>>
我的友情链接
查看>>
python基础---高阶函数
查看>>
ftp command
查看>>
Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群
查看>>
linux下安装无线网卡
查看>>
二级指针
查看>>
php中导入导出csv文件
查看>>
Linux(Centos)安装配置SVN服务器
查看>>
BBasic-Diary921
查看>>
记忆化搜索(例)
查看>>
10.30T1 期望DP
查看>>
在VMware上安装CentOS-6.5 minimal - 配置网络
查看>>