排序-链式基数排序
来源:互联网 发布:淘宝房产网加盟 编辑:程序博客网 时间:2024/05/18 02:53
输入格式:
输入第一行给出正整数N(≤105),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。
输出格式:
在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。
输入样例:
114 981 10 -17 0 -20 29 50 8 43 -5
输出样例:
-20 -17 -5 0 4 8 10 29 43 50 981
/* 链式基数排序 ❥(ゝω・✿ฺ) ❥(ゝω・✿ฺ)*//* 这题的关键是将负数的处理 将余数为-9 ~ -1 转化为 0 - 8 而余数为 0 - 9转化为 9 - 18*/#include <bits/stdc++.h>#define MIN 0xFFFFFFFDusing namespace std;typedef struct Node{ int data; struct Node *Next;}Node, *NodePtr;typedef struct SLList{ Node *head, *tail;}SLList;int GetKeySize(int n){ int cnt = 0; while(n != 0) { n /= 10; ++cnt; } return cnt;}int Ord(int val, int i){ int divisor = 1; for(int n = 1; n < i; ++n) divisor *= 10; if(abs(val) / divisor < 10) return (val / divisor) % 10; else return (abs(val) / divisor) % 10;}void Distribute(SLList &L, SLList *temp, int n){ NodePtr node = L.head; NodePtr tmpPtr; int pos; while(node != NULL) { tmpPtr = node->Next; node->Next = NULL; pos = Ord(node->data, n) + 9; if(temp[pos].head == NULL) { temp[pos].head = temp[pos].tail = node; } else { temp[pos].tail->Next = node; temp[pos].tail = node; } node = tmpPtr; } L.head = L.tail = NULL;//这里要置空}void Collect(SLList &L, SLList *temp){ for(int i = 0; i < 19; ++i) { if(temp[i].head != NULL) { if(L.head == NULL) { L.head = temp[i].head; L.tail = temp[i].tail; } else { L.tail->Next = temp[i].head; L.tail = temp[i].tail; } } temp[i].head = temp[i].tail = NULL;//这里要置空 }}void RadixSort(SLList &L, int KeySize){ SLList temp[25]; for(int i = 0; i < 25; ++i) { temp[i].head = temp[i].tail = NULL; } for(int i = 1; i <= KeySize; ++i) { Distribute(L, temp, i); Collect(L, temp); NodePtr cur = L.head; }}int main(){ int N, val, pos, KeySize, MAX = MIN; SLList L; L.head = L.tail = NULL; scanf("%d", &N); for(int i = 1; i <= N; ++i) { NodePtr node = (NodePtr)malloc(sizeof(Node)); scanf("%d", &val); node->data = val; node->Next = NULL; if(i == 1) { L.tail = L.head = node; } else { L.tail->Next = node; L.tail = L.tail->Next; } if(abs(val) > MAX) MAX = abs(val); } KeySize = GetKeySize(MAX); RadixSort(L, KeySize); NodePtr cur = L.head; printf("%d", cur->data); cur = cur->Next; while(cur != NULL) { printf(" %d", cur->data); cur = cur->Next; }}
阅读全文
0 0
- 排序-链式基数排序
- 数据结构_内部排序_链式基数排序
- C++代码,数据结构-内部排序-基数排序-链式基数排序
- 链式基数排序
- 链式基数排序
- 链式基数排序
- 链式基数排序
- 基数排序--链式基数排序(Radix)
- 基数排序与链式基数排序
- 数据结构 JAVA描述(十二) 归并排序 链式基数排序
- 【Data_Structure笔记7】排序算法之【链式基数排序】
- 链式队列实现基数排序
- 链式基数排序_对整数序列进行排序,低关键字优先的基数排序算法很有创新。
- 【排序】基数排序
- 【排序】基数排序
- 【排序】基数排序
- 排序 基数排序
- 数据结构——链式基数排序
- Unity双指触控缩放视野
- Android——RecyclerView——ViewHolder——源码注释翻译:ViewHolder的官方
- python 几个内置函数的用法
- 日常运维-3
- 简单爬虫(2):火车信息
- 排序-链式基数排序
- 人脸3D重建
- NullReferenceException,就不应该存在!
- Queue
- 当我们使用 MVVM 模式时,我们究竟在每一层里做些什么?
- Hyperledger Fabric v1.0开发环境配置
- 蓝桥杯 算法训练 暗恋
- ubuntu 国内源
- cin.getline()和getline()的区别联系