initial
[prop.git] / include / AD / sort / bubble2.h
blob29cd20da41b13018849151e721db2b9c7fcb1472
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef bubble_sort2_h
26 #define bubble_sort2_h
28 #include <AD/sort/sorting2.h>
31 // Lazy bubble sort, for those who don't care for anything better.
33 // e.g.: bubbleSort2(char *, int, names, salaries, 100, strcmp(names[i], names[i+1]) < 0);
36 /////////////////////////////////////////////////////////////////////////////
37 // Macro version
38 /////////////////////////////////////////////////////////////////////////////
39 #define bubbleSort2(KEY,VALUE,A,B,N,predicate) \
40 do { \
41 for (register int j = (N) - 1; j >= 0; j--) { \
42 for (register int i = 0; i < j; i++) { \
43 if (! (predicate)) { \
44 register KEY key = A[i]; \
45 register VALUE value = B[i]; \
46 A[i] = A[i+1]; A[i+1] = key; \
47 B[i] = B[i+1]; B[i+1] = value; \
48 } \
49 } \
50 } \
51 } while(0)
53 /////////////////////////////////////////////////////////////////////////////
54 // Template version
55 /////////////////////////////////////////////////////////////////////////////
56 template <class K, class V, class Ord>
57 class BubbleSort2 : public Sorting2<K,V,Ord> {
58 public:
59 void sort(int, K [], V []);
62 template <class K, class V, class Ord>
63 void BubbleSort2<K,V,Ord>::sort(int N, K keys[], V values[])
64 { bubbleSort2(K, V, keys, values, N, Ord::less(keys[i], keys[i+1])); }
66 #endif