170 likes | 185 Views
Bin sort is a faster way to accomplish sorting by distributing nodes into bins and then linking the bins into a single sorted chain. This method involves deleting nodes from the input chain and adding them to the appropriate bin.
E N D
Chapter 3 DATA REPRESENTATION(3) L.Chen
3.8 APPLICATIONS 3.8.1 Bin Sort(箱子排序) • It is a faster way to accomplish the sort. • For bin sort we need to be able to • move down the input chain deleting nodes from this chain and adding them to the chain for the appropriate bin and • collect and link chains from the bins into a single sorted chain. L.Chen
class Node { friend ostream& operator << (ostream&, const Node &); public : int operator != (Node x) const { return (score != x.score); } private : int score; char *name; }; ostream& operator << (ostream& out, const Node& x) { out << x.score << ' '; return out; } Program 3.40 Possible node class for bin sort
An alternative to overloading is to provide a conversion from the type Node to a numeric type that can be used for comparison and output purposes. class Node { public : // overload type conversion operator operator int ( ) const { return score; } private : int score; char *name; }; Program 3.41 An alternative way to handle operator overloading
We can combine both overloading approaches so that type conversion to int occurs only when the operator will fail without the type conversion. So we may use the definition of program 3.42 class Node { friend ostream& operator << (ostream&, const Node &); public: int operator != (Node x) const { return (score != x.score || name[0] != x.name[0]); } operator int ( ) const { return score; } private: int score; char *name; }; ostream& operator << (ostream& out,const Node& x) { out << x.score << ' ' << x.name[0] << ' '; return out; } Program 3.42 Another way to handle operator overloading
The complexity of BinSort is Θ(n+range) void BinSort(Chain<Node>& X, int range) { int len = X.Length(); // Sort by score Node x; Chain<Node> *bin; bin = new Chain<Node> [range + 1]; for (int i = 1; i <= len; i++) // distribute to bins { X.Delete(1,x);bin[x.score].Insert(0,x);} for (int j = range; j >= 0; j--) //collect from bins while (!bin[j].IsEmpty()) { bin[j].Delete(1,x); x.Insert(0,x); } delete [ ] bin; } Program 3.43 Bin sort using class denitions
template <class T> void Chain<T> : : BinSort (int range) { int b; ChainNode<T> **bottom, **top; bottom = new ChainNode<T>* [range + 1]; top = new ChainNode<T>* [range + 1]; for (b = 0; b <= range; b++) bottom[b] = 0; for (; first; first = first->link) { b = first->data; if (bottom[b]) { top[b]->link = first; top[b] = first; } else bottom[b] = top[b] = first; }
ChainNode<T> *y = 0; for (b = 0; b <= range; b++) if (bottom[b]) {// bin not empty if (y) y->link = bottom[b]; else first = bottom[b]; y = top[b]; } if (y) y->link = 0; delete [ ] bottom; delete [ ] top; } Program 3.44 Bin sort as a class member of Chain
inline int F1(Node& x) { return x.exam1; } inline int F2(Node& x) { return x.exam2; } inline int F3(Node& x) { return x.exam1 + x.exam2 + x.exam3; } void main(void ) { Node x; Chain<Node> L; randomize(); for (int i = 1; i <= 20; i++) { x.exam1 = i/2; x.exam2 = 20 - i; x.exam3 = random(100); x.name = i; L.Insert(0,x); } L.BinSort(10, F1);
cout << "Sort on exam 1" << endl; cout << L << endl; L.BinSort(20, F2); cout << "Sort on exam 2" << endl; cout << L << endl; L.BinSort(130, F3); cout << "Sort on sum of exams" << endl; cout << L << endl; } Program 3.45 Sorting on different fields
3.8.2 Radix Sort(基数排序) L.Chen
void Initialize(int n) {// Initialize n classes with one element each. e = new int [n + 1]; for (int e = 1; e <= n; e++) e[e] = e; } void Union(int i, int j) {// Union the classes i and j. for (int k = 1; k <= n; k++) if (E[k] == j) E[k] = i; } int Find(int e) { // Find the class that contains element i. return E[e]; } Program 3.46 Online equivalence class functions using arrays
void Initialize(int n) { // Initialize n classes with one element each. node = new EquivNode [n + 1]; for (int e = 1; e <= n; e++) { node[e].E = e; node[e].link = 0; node[e].size = 1; } } void Union(int i, int j) { if (node[i].size > node[j].size) Swap(i,j); int k; for (k = i; node[k].link; k = node[k].link) node[k].E = j; node[k].E = j; node[j].size += node[i].size; node[k].link = node[j].link; node[j].link = i; } int Find(int e) { return node[e].e; } Program 3.47 Online equivalence class
Exercises L.Chen