基数排序的C语言实现
来源:互联网 发布:曾经最火的网络歌曲 编辑:程序博客网 时间:2024/05/22 12:28
#include <stdio.h>#include <string>#define N 10//每位数据取值范围0~9共计10#define NUM 3 //每个数据由三位数字组成typedef struct {int front;int rear;} Queue;//定义“桶”Queue queue[N];typedef struct {int data;int next;}List;//获取一个整数的位数int get_figure_num(int number){int temp = number;int count = 0;while(temp > 10) {count++;temp /= 10;}count++;return count;}//按位提取数字int extract_number(int number, int num) {int temp;switch(num) {case 0:temp = number -(number/10)*10;break;case 1:temp = number%100/10;break;case 2:temp = number/100;break;default:;}return temp;}//按某位数字将数据分配到相应桶中void distribute_by_certain_num(List list[], int n, int head, int certain_num){int temp;for(int i = head; i != -1; i = list[i].next) {temp = extract_number(list[i].data, certain_num);if(-1 == queue[temp].front) {queue[temp].front = i;queue[temp].rear = i;}else {list[queue[temp].rear].next = i;queue[temp].rear = i;//注释1:更改队列尾指针}}}//将桶中的数据按队列的顺序收集int collect_by_queue(List list[], int n) {int head = 0; int p = 0;int j = 0;while(-1 == queue[j].front) {j++;}head = queue[j].front;p = queue[j].rear;for(int i = j + 1; i < n; ++i) {if(queue[i].front != -1) {list[p].next = queue[i].front;p = queue[i].rear;}}list[p].next = -1;//注释2:***清空队列***非常重要的步骤!for(int i = 0; i < N; ++i) {queue[i].front = -1;queue[i].rear = -1;}return head;}//三位数的基数排序void radix_sort(List list[], int n) {int head = 0;for(int i = 0; i < N; ++i) {queue[i].front = -1;queue[i].rear = -1;}for(int i = 0; i < NUM; ++i) {distribute_by_certain_num(list, n, head, i);head = collect_by_queue(list, n);}for(int i = head; i != -1; i = list[i].next) {printf("%5d", list[i].data);}printf("\n");}int main() {List list[10];for(int i = 0; i < 10; ++i) {list[i].data = rand() % 900 + 100 - i;//随机生成3位随机数if(i < 9) {list[i].next = i + 1;}else {list[i].next = -1;}}radix_sort(list, 10);return 0;}
整个程序实现Debug最耗时的地方就是注释1,2那几句,在编程的过程中真是一不小心就会疏忽,导致一些问题出现。
0 0
- 基数排序的C语言实现
- 基数排序C语言实现
- 基数排序-C语言实现
- 基数排序 C语言实现
- 基数排序-C语言实现
- 基数排序(基于C语言的实现)
- 基数排序及C语言实现
- 基数排序单链表实现(C语言)
- 基数排序C语言代码实现
- 基数排序的C实现
- C语言实现基数排序——针对数字的实现方法
- 基数排序 C语言数据结构
- 桶排序升级之基数排序c语言实现10.1.2
- 基数排序(C语言版本)
- 基数排序C++实现
- 基数排序的java实现
- 基数排序的完整实现
- 基数排序的 Java 实现
- 第七周撞错体验
- 客户端请求新页面和提交数据的四种方式
- 判断出栈顺序
- UI 视图编程入门
- 第7周 项目3-2 简单分支问题体验
- 基数排序的C语言实现
- ZOJ 3805 Machine
- sdut 3-6 静态数据成员与静态成员函数
- wamp 虚拟目录的设置
- Mac OS使用技巧之八:Dock栏使用技巧
- Task和Back Stack(一)
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- 黑马程序员------foundation_NSArray,NSMutableArray
- Mac OS使用技巧之九:Mission Control和DIY自己的Dashboard