最近不知道搞什么,很慢,太无语了。
桶排序定义:
N个数,区间在[0,1),把这段区间划分成N份,然后把这N个数依次放到这N份区间中去,注意这里每份空间都代表一个桶,然后对桶里面的元素进行排序,最后把桶结合起来就成了有序的数列。
在这里我把区间设为[min,max+1),原理是一样的,其实桶排序的实现是从哈希表来的,后面有时间按会实现hash表的。
源码:
#ifndef BUCKET_SORT_H #define BUCKET_SOET_H #include<iostream> using namespace std; template<typename T> struct node { T value; node* next_node; node(T key):value(key),next_node(0){} }; template<typename T> class Bucket_sort { private: T min_value;//区间的下限 MIN T max_value;//区间的上限 MAX int num;//桶的数目 int interval;//每个桶的区间间隔 public: typedef typename node<T>* PNODE; protected: PNODE* node_map;//指向PNODE 指针的内存区域,用于映射 每段区域的头节点,指向桶的指针 public: Bucket_sort(T min=0,T max=100,int number=100):min_value(min),max_value(max+1),num(number) { this->interval=(this->max_value-this->min_value)/this->num; node_map=(PNODE*)calloc(this->num,sizeof(PNODE));//分配映射空间 } private://负责内存分配和释放 PNODE create_node(T value) { PNODE new_node=(PNODE)calloc(1,sizeof(node<T>));//分配内存 new(new_node) node<T>(value);//构造内存 return new_node; } void dealloc_node(PNODE pNode)//释放内存 { free(pNode); } private: void insert(PNODE& pHead,PNODE pNode)//插入节点 { if(0==pHead) { pHead=pNode; return ; } if(pHead->value>pNode->value) { //PNODE tempNode=pHead->next_node; pNode->next_node=pHead; pHead=pNode; return ; } if(pHead->value<=pNode->value) return insert(pHead->next_node,pNode); } void earser(PNODE& pHead,T value)//删除节点 { if(pHead==0) return ; if(pHead->value>value) return ; if(pHead->value==value) { PNODE tempNode=pHead; pHead=pHead->next_node; this->dealloc_node(tempNode); return earser(pHead,value); } if(pHead->value<value) return earser(pHead->next_node,value); } public: void insert(T value)//插入数据 { PNODE new_node=this->create_node(value); int position=(new_node->value-this->min_value)/this->interval; return insert(node_map[position],new_node); } void earser(T value)//删除数据 { int index=(value-this->min_value)/this->interval; earser(this->node_map[index],value); } T getMin()//得到最小值 { //int index=0; PNODE* ptempNode=this->node_map; while(*ptempNode==0&&ptempNode<(this->node_map+this->num)) ++ptempNode; if(ptempNode>=(this->node_map+this->num)) return this->min_value; else return (*ptempNode)->value; } T getMax()//得到最大值 { PNODE* ptempNode=this->node_map+this->num-1; PNODE tempNode; while(*ptempNode==0&&ptempNode>=(this->node_map)) --ptempNode; T tempValue; tempNode=*ptempNode; while(tempNode->next_node!=0) { tempValue=tempNode->value; tempNode=tempNode->next_node; } return tempNode->value; } void display()//罗列出所有的数据 { for(int i=0;i!=this->num;++i) { PNODE tempNode=this->node_map[i]; while(tempNode) { cout<<tempNode->value<<" "; tempNode=tempNode->next_node; } //cout<<endl; } } }; #endif
测试代码:
#include "bucket_sort.h" #include<time.h> #include<stdlib.h> #include<iostream> using namespace std; int main() { srand((int)time(0)); Bucket_sort<int> iBucket(0,999,100); for(int i=0;i<100;++i) iBucket.insert(rand()00); iBucket.display(); cout<<endl<<endl; for(int i=0;i<100;++i) iBucket.earser(i); iBucket.display(); cout<<endl<<"max="<<iBucket.getMax()<<endl<<"min="<<iBucket.getMin()<<endl; system("pause"); return 0; }