-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.cpp
219 lines (194 loc) · 5.64 KB
/
sort.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <benchmark/benchmark.h>
#include <vector>
#include <numeric>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <experimental/random>
#define FIXTURE_BECHMARK_NAME Sort
class FIXTURE_BECHMARK_NAME : public ::benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) BENCHMARK_OVERRIDE {
if (state.thread_index() == 0) {
generateData(state.range(0));
}
}
void TearDown(const ::benchmark::State& state) BENCHMARK_OVERRIDE {
if (state.thread_index() == 0) {
data.clear();
}
}
void bubbleSort()
{
for (size_t i = 0; i < ret.size()-1; ++i) {
for (size_t j = 1; j < ret.size()-i; j++)
{
if (ret[j-1]>ret[j])
{
std::swap(ret[j-1],ret[j]);
}
}
}
}
void selectSort()
{
size_t pos ;
long tmpmin;
for (size_t i = 0; i < ret.size()-1; ++i) {
pos = i;
tmpmin=ret[i];
for (size_t j = i+1; j < ret.size(); j++)
{
if (ret[j]<tmpmin)
{
tmpmin = ret[j];
pos=j;
}
}
if (i!=pos)
{
std::swap(ret[pos],ret[i]);
}
}
}
void insertSort()
{
size_t pos ;
long tmpmin;
for (int i = 0; i < ret.size()-1; ++i) {
for (int j = i+1; j > 0; j--)
{
if (ret[j]<ret[j-1])
{
std::swap(ret[j],ret[j-1]);
}
else{
break;
}
}
}
}
void countSort()
{
//std::fill(tmp.begin(),tmp.end(),0);
auto sz=ret.size();
for (size_t i = 0; i < sz; ++i) {
tmp[ret[i]]++;
}
int pos=0;
for (int i = 0; i < sz; i++)
{
while (tmp[i]--)
{
ret[pos++]=i;
}
}
}
void quickSort()
{
iterSort(ret,0,ret.size()-1);
}
protected:
void printResult()
{
for (auto value:data)
{
std::cout<<value<<" ";
}
std::cout<<std::endl;
long last=0;
for (auto value:ret)
{
if (last>value)
{
std::cout<<last<<">";
}
std::cout<<value<<" ";
last = value;
}
std::cout<<std::endl;
}
void generateData(int count)
{
data.resize(count);
std::srand(std::time(nullptr)); // use current time as seed for random generator
for (auto &value: data)
{
value = std::experimental::randint(0, count-1);
}
}
void copyData()
{
ret = data;
tmp.resize(ret.size());
std::fill(tmp.begin(),tmp.end(),0);
}
int partition(std::vector<int>& ret,int low,int high)const
{
auto pivot = ret[low];
while (low < high)
{
while (low < high && ret[high] > pivot)
{
high--;
}
ret[low] = ret[high];
while (low < high && ret[low] <= pivot)
{
low++;
}
ret[high] = ret[low];
}
ret[low] = pivot;
return low;
}
void iterSort(std::vector<int>& ret,int low,int high)const
{
if (low < high) {
auto pivot = partition(ret,low, high);
iterSort(ret,low, pivot - 1);
iterSort(ret,pivot + 1, high);
}
return;
}
decltype(&FIXTURE_BECHMARK_NAME::bubbleSort) method[5]={
&FIXTURE_BECHMARK_NAME::bubbleSort
,&FIXTURE_BECHMARK_NAME::selectSort
,&FIXTURE_BECHMARK_NAME::insertSort
,&FIXTURE_BECHMARK_NAME::countSort
,&FIXTURE_BECHMARK_NAME::quickSort
};
std::vector<int> data;
std::vector<int> ret;
std::vector<int> tmp;
};
BENCHMARK_DEFINE_F(FIXTURE_BECHMARK_NAME, benchSort)(benchmark::State& state)
{
auto m = method[state.range(1)];
for ([[maybe_unused]] auto _ : state) {
state.PauseTiming();
copyData();
state.ResumeTiming();
(this->*m)();
}
state.SetComplexityN(state.range(0));
}
BENCHMARK_DEFINE_F(FIXTURE_BECHMARK_NAME, benchPrint)(benchmark::State& state)
{
auto m = method[state.range(1)];
for ([[maybe_unused]] auto _ : state) {
copyData();
(this->*m)();
}
printResult();
}
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchSort)->RangeMultiplier(5)->Ranges({{10,10000},{0,0}})->Complexity(benchmark::oAuto);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchSort)->RangeMultiplier(5)->Ranges({{10,10000},{1,1}})->Complexity(benchmark::oAuto);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchSort)->RangeMultiplier(5)->Ranges({{10,10000},{2,2}})->Complexity(benchmark::oAuto);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchSort)->RangeMultiplier(2)->Ranges({{1000,10000},{3,3}})->Complexity(benchmark::oN);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchSort)->RangeMultiplier(5)->Ranges({{10,10000},{4,4}})->Complexity(benchmark::oAuto);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchPrint)->Args({10,0})->Iterations(1);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchPrint)->Args({10,1})->Iterations(1);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchPrint)->Args({10,2})->Iterations(1);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchPrint)->Args({10,3})->Iterations(1);
//BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, benchPrint)->Args({10,4})->Iterations(1);