-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaBubbleSort.h
More file actions
52 lines (47 loc) · 881 Bytes
/
Copy pathMetaBubbleSort.h
File metadata and controls
52 lines (47 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef _META_BUBBLE_SORT_H_
#define _META_BUBBLE_SORT_H_
template<class T, int Size, int Index>
class MetaBubbleSortLoop
{
private:
enum { go = (Index <= Size - 2) };
public:
static void loop(T * data)
{
if (data[Index] > data[Index + 1])
{
T temp = data[Index];
data[Index] = data[Index + 1];
data[Index + 1] = temp;
}
MetaBubbleSortLoop<T, go ? Size : 0, go ? (Index + 1) : 0>::loop(data);
}
};
template<class T>
class MetaBubbleSortLoop<T, 0, 0>
{
public:
static void loop(T * data)
{
}
};
template<class T, int N>
class MetaBubbleSort
{
static_assert(N > 0, "Array size has to be positive");
public:
static void sort(T * data)
{
MetaBubbleSortLoop<T, N - 1, 0>::loop(data);
MetaBubbleSort<T, N - 1>::sort(data);
}
};
template<class T>
class MetaBubbleSort<T, 1>
{
public:
static void sort(T * data)
{
}
};
#endif // _META_BUBBLE_SORT_H_