1 //////////////////////////////////////////////////////////////////////////////
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
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
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
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 /////////////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////////////
39 #define bubbleSort2(KEY,VALUE,A,B,N,predicate) \
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; \
53 /////////////////////////////////////////////////////////////////////////////
55 /////////////////////////////////////////////////////////////////////////////
56 template <class K
, class V
, class Ord
>
57 class BubbleSort2
: public Sorting2
<K
,V
,Ord
> {
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])); }