箱子排序算法

template<class T> void Chain<T>::BinSort(int range, int(*value)(T& x)) { int i, j; ChainNode<T> **bottom, **top; //初始化箱子 bottom = new ChainNode<T>* [range + 1]; top = new ChainNode<T>* [range + 1]; for (i = 0; i <= range; i++) bottom[i] = 0; //把节点分配到各个箱子里 for (; first; first = first->link) { j = value(first->data); if (bottom[j]) {//箱子非空 top[j]->link = first; top[j] = first;} else //箱子为空 bottom[j] = top[j] = first; } //搜集各个箱子中的元素,产生一个排序链表 ChainNode<T> *y = 0; for (i = 0; i <= range; i++) if (bottom[i]) {//箱子非空 if (y) //不是第一个非空的箱子 y->link = bottom[i]; else //第一个非空箱子 first = bottom[i]; y = top[i];} if (y) y->link = 0; delete [] bottom; delete [] top; }

你可能感兴趣的:(算法,delete)