This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teasort.cc
140 lines (125 loc) · 2.98 KB
/
teasort.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iterator>
#include <numeric>
#include <random>
#include <vector>
template <typename T>
struct Vertex
{
typedef std::vector<Vertex*> Neighbours;
size_t id;
T value;
bool visited;
Neighbours neighbours;
inline void visit() { visited = true; }
Vertex() { id = 0; visited = false; }
};
template <typename T>
struct Graph
{
typedef typename std::vector<Vertex<T>>::iterator iterator;
size_t size;
std::vector<Vertex<T>> vertices;
Graph(size_t nsize)
: vertices(nsize)
{
size = nsize;
for (size_t i = 0; i < size; i++)
vertices[i].id = i;
}
inline iterator begin(void) { return vertices.begin(); }
inline iterator end(void) { return vertices.end(); }
inline Vertex<T> &operator[] (size_t n) { return vertices[n]; }
inline void add_edge(size_t x, size_t y)
{
assert(x < size && y < size);
vertices[x].neighbours.push_back(&vertices[y]);
}
};
template <typename T>
void dfs_visit(Vertex<T> &u, std::vector<Vertex<T>*> &result)
{
assert(!u.visited);
u.visit();
for (auto vpp = u.neighbours.begin(); vpp != u.neighbours.end(); vpp++)
{
Vertex<T> &v = **vpp;
if (!v.visited)
dfs_visit(v, result);
}
result.push_back(&u);
}
template <typename T>
std::vector<Vertex<T>*> dfs(Graph<T> &graph)
{
std::vector<Vertex<T>*> result;
for (auto vp = graph.begin(); vp < graph.end(); vp++)
{
if (!vp->visited)
dfs_visit(*vp, result);
}
return result;
}
uintmax_t teasort(int *p, int *q)
{
size_t n = q - p;
uintmax_t m = static_cast<uintmax_t>(2) * n * std::ilogb((double)n);
static std::random_device rdev;
static std::default_random_engine rng(rdev());
std::uniform_int_distribution<> rand(0, n - 1);
Graph<int> graph(n);
for (size_t i = 0; i < n; i++)
graph[i].value = p[i];
for (size_t i = 0; i < m; i++)
{
size_t x = rand(rng);
size_t y = rand(rng);
if (p[y] > p[x])
std::swap(x, y);
graph.add_edge(x, y);
}
std::vector<Vertex<int>*> result = dfs(graph);
assert(result.size() == n);
std::vector<int> t(n + 1);
t[0] = std::numeric_limits<int>::min();
for (size_t i = 0; i < n; i++)
t[i + 1] = result[i]->value;
for (size_t i = 2; i <= n; i++)
{
size_t j = i;
int v = t[i];
while (m++, t[j - 1] > v) {
t[j] = t[j - 1];
j--;
}
t[j] = v;
}
std::copy(t.begin() + 1, t.end(), p);
return m;
}
#define TEST_MIN (1 << 5)
#define TEST_ITER 16
int main(void)
{
for (size_t n = TEST_MIN; ; n *= 2)
{
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 1);
uintmax_t cost = 0;
for (int i = 0; i < TEST_ITER; i++) {
std::random_shuffle(p.begin(), p.end());
cost += teasort(&p[0], &p[0] + n);
for (size_t k = 0; k < n; k++)
assert(p[k] == k + 1);
}
double fcost = (double)cost / (double)TEST_ITER;
printf("%8d\t%8.2fN\n", n, fcost / n);
fflush(stdout);
}
return 0;
}
// vim:ts=2 sts=2 sw=2 et