1 / 17

Bin Sort: A Faster Sorting Method

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.

Download Presentation

Bin Sort: A Faster Sorting Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 DATA REPRESENTATION(3) L.Chen

  2. 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

  3. L.Chen

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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; }

  9. 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

  10. 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);

  11. 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

  12. 3.8.2 Radix Sort(基数排序) L.Chen

  13. 3.8.3 Equivalence Classes(等价类) L.Chen

  14. L.Chen

  15. 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

  16. 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

  17. Exercises L.Chen

More Related