This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec_sort_basic.cpp
More file actions
170 lines (90 loc) · 2.08 KB
/
ec_sort_basic.cpp
File metadata and controls
170 lines (90 loc) · 2.08 KB
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
#include <iostream>
#include <random>
#include <functional>
const int Size = 10;
typedef std::function<bool (int const&, int const&)> Comparer;
void MakeRandom(int* data, int count);
void PrintArray(int* data, int count);
void BubbleSort(int* data, int count, Comparer);
void InsertSort(int* data, int count, Comparer);
void SelectSort(int* data, int count, Comparer);
template<typename Function>
void Case(Function f)
{
int dataset[Size];
puts("---------------------------");
puts("original array :");
MakeRandom(dataset, Size);
PrintArray(dataset, Size);
std::less<int> less;
std::greater<int> greater;
puts("sorted by less() :");
BubbleSort(dataset, Size, less);
PrintArray(dataset, Size);
puts("sorted by greater() :");
BubbleSort(dataset, Size, greater);
PrintArray(dataset, Size);
puts("");
}
int main()
{
puts("func: bubble");
Case(BubbleSort);
puts("func: insertion");
Case(InsertSort);
puts("func: selection");
Case(SelectSort);
}
void PrintArray(int* data, int count)
{
printf("[");
for (int i = 0 ; i < count; ++i)
{
if (i != 0)
printf(", ");
printf("%d", data[i]);
}
printf("]\n");
}
void MakeRandom(int* data, int count)
{
using namespace std;
random_device rd;
default_random_engine e1(rd());
uniform_int_distribution<int> uniform_dist(1,100);
for (int i = 0; i < Size; ++i)
data[i] = uniform_dist(e1);
}
void BubbleSort(int* data, int count, Comparer f)
{
for (int i = 0; i < count; ++i)
for (int j = 0; j < count; ++j)
{
if (f(data[i], data[j]))
std::swap(data[i], data[j]);
}
}
void SelectSort(int* data, int count, Comparer comp)
{
int currentTarget = *data;
for (int i = 0; i < count; ++i)
{
for (int j = i; j < count; ++j)
{
if (comp( currentTarget, data[j] ))
currentTarget = data[j];
}
data[i] = currentTarget;
}
}
void InsertSort(int* data, int count, Comparer comp)
{
for (int i = 1 ; i < count; ++i)
{
int temp = data[i];
int j = i - 1;
for (; 0 <= j && comp(data[j], temp); --j)
data[j + 1] = data[j];
data[j + 1] = temp;
}
}