# in clu de <cst dli b>
# in clu de <iostream>
# in clu de <vector>
using namespace std ;
// The c l a s s IntSwap<I , J>i s a s i m p le c l a s s c o n t a i n i n g a f u n c t i o n
// f o r swapping i n t e g e r s a t i n d i c e s I and J i n an a r r a y i f n e c e s s a r y
template <int I , int J>class I ntS wap {
public:
stat ic inlin e void compareAndSwap(int ∗data) {
if ( dat a [I ] >da ta [ J] )
swa p ( da ta [ I] , d ata [ J]);
}
};
// The class IntBubbleSortLoop<I , J>w i l l swap t h e e l em e nt a t i n d e x J
// w it h t h e e l em e nt a t i n d e x J+1 i f n e c e s s a r y , t he n go on u n t i l i t
// r e a c he s t he end o f t he a rr ay , whose l e n g th i s r e p r e s e n t e d by I .
template <int I , int J>class IntBubbleSortLoop {
pr iva te :
enum {go = ( J <= I−2 ) };
public:
stat ic inl ine void loop(int ∗data) {
cout << " Loop " << I<< " " << J<< endl;
In tSw ap<J , J +1>:: c om par eA ndSwa p ( dat a );
IntBubbleSortLoop<go ? I : 0 , go ? (J +1) : 0 >:: loop ( dat a );
}
};
template <> class IntBubbleSortLoop<0 ,0>{
public:
stat ic inl ine void loop(int ∗){ }
};
// The c l a s s I n t B u b b l e S o r t<N>i s a c l a s s c o n t a i n i n g a f u n c t i o n s o r t t h at
// a p p l y s a b u bb le s o r t t o a g i v e n a r r a y .
template <int N>struct IntBubbleSort {
stat ic inl ine void sort(int ∗data) {
IntBubbleSortLoop<N−1,0>:: loop ( dat a );
IntBubbleSort<N−1>:: s ort ( dat a );
}
};
template <> struct IntBubbleSort<1>{
stat ic inl ine void sort(int ∗data) { }
};
int main(void){
int in t_li st [10] = {10 ,7 ,4 ,8 ,2 ,5 ,3 ,1 ,9 ,6 };
IntBubbleSort<10>:: sort ( int _li st );
for (int i =0; i<10; i++)
cout << int_list[i] << ’ ’;
cout << endl;
return EXIT_SUCCESS;
}
5