Download presentation
Presentation is loading. Please wait.
Published byAlberta Martin Modified over 9 years ago
1
2004-03L.Chen Chapter 3 DATA REPRESENTATION(3)
2
2004-03L.Chen 3.8 APPLICATIONS 3.8.1 Bin Sort 3.8.1 Bin Sort (箱子排序) It is a faster way to accomplish the sort. For bin sort we need to be able to 1) move down the input chain deleting nodes from this chain and adding them to the chain for the appropriate bin and 2) collect and link chains from the bins into a single sorted chain.
3
2004-03L.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 & X, int range) { int len = X.Length(); // Sort by score Node x; Chain *bin; bin = new Chain [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 void Chain : : BinSort (int range) { int b; ChainNode **bottom, **top; bottom = new ChainNode * [range + 1]; top = new ChainNode * [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 *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 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
2004-03L.Chen 3.8.2 Radix Sort (基数排序)
13
2004-03L.Chen 3.8.3 Equivalence Classes (等价类)
14
2004-03L.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
2004-03L.Chen Exercises
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.