C++基数排序(清楚明了完美详细的实现)
2015-06-16 18:03
363 查看
#include <iostream> #define _MAX_SIZE 9 static const int b[_MAX_SIZE] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; using namespace std; int GetSetNumber(int x) { for (int i = 0; i < 9; i++) { if (x>=b[i] && b[i + 1] > x)//查表得到位数,这是求数字位数的比较快速的方法了。 return i+1; } } struct Node { Node *next; int data;//存储的数据。 Node(int d = int()) :data(d), next(NULL){} }; struct MyNode { Node *adj; int data;//表示位数。 MyNode(int d = int()) :data(d), adj(NULL){} }; class HashNode { public: HashNode(int d = _MAX_SIZE) { table = new MyNode[_MAX_SIZE]; } void Grial(int a[],int n) { for (int i = 0; i < n; i++) { int index = GetSetNumber(a[i]); Node *p = table[index].adj; Node *m = NULL; Node *s = new Node(a[i]); while (p != NULL && a[i]>p->data) { m = p; p = p->next; } if (m == NULL) { s->next = table[index].adj; table[index].adj = s; continue; } s->next = m->next; m->next = s; } } void Printf() { for (int i = 0; i < _MAX_SIZE; i++) { Node *p = table[i].adj; while (p != NULL) { cout << p->data << " "; p = p->next; } } } private: MyNode *table; }; //先将位数进行排序。 //然后再将相同位数的再进行插入排序,HASH表超级完美的实现了, //是不是比那些书上写的写法简单多了呢?并且时间效率只要o(n), //空间效率也不太大。 int main() { int a[] = {1,0,33,2,555,4,9,18}; HashNode sh; sh.Grial(a,8); sh.Printf(); return 0; }
相关文章推荐
- 为游戏多开做准备,降低游戏的CPU
- float的内存布局
- c++ Builder 死机解决方法
- C++类中:作用
- C++ 自定义控件的移植(将在其它程序中设计的自定义控件,移植到现在的系统中)
- C++程序中获取不带文件路径和后缀的文件名
- c++ builder GroupBox的使用方法,如何把控件拖到TGroupBox上面
- 研究生本科生的创建C++链表
- C++带参数的类成员函数
- 您的服务器组件没有得到合法授权,服务器将会以受限模式运行
- C++中静态成员函数访问非静态成员变量
- 【C++ Primer Plus 9.3】名称空间
- 浅析C++中的this指针
- 《C语言 学生成绩管理系统》
- [LeetCode] Rectangle Area
- C++标准库笔记-1
- <Effective C++>读书笔记-2
- C++学习 boost::optional详解
- C语言学习笔记(三)——数组
- c语言移位操作