質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -79,3 +79,89 @@
|
|
79
79
|
}
|
80
80
|
|
81
81
|
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
---
|
86
|
+
|
87
|
+
## 解決後追記
|
88
|
+
|
89
|
+
ソートの実装そのものにも誤りがあったので、正しく機能するもののコードを残しておきます。
|
90
|
+
|
91
|
+
```C++
|
92
|
+
|
93
|
+
https://wandbox.org/permlink/FpGavGk9jUIKGQkk
|
94
|
+
|
95
|
+
#include <algorithm>
|
96
|
+
|
97
|
+
#include <functional>
|
98
|
+
|
99
|
+
#include <iostream>
|
100
|
+
|
101
|
+
#include <iterator>
|
102
|
+
|
103
|
+
#include <vector>
|
104
|
+
|
105
|
+
using Iterator = std::vector<int>::iterator;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
void BubbleSort(Iterator begin, Iterator end) {
|
110
|
+
|
111
|
+
while (true) {
|
112
|
+
|
113
|
+
auto it = begin;
|
114
|
+
|
115
|
+
auto is_swapped = false;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
while (std::next(it) != end) {
|
120
|
+
|
121
|
+
if (*it > *std::next(it)) {
|
122
|
+
|
123
|
+
std::iter_swap(it, std::next(it));
|
124
|
+
|
125
|
+
is_swapped = true;
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
++it;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
if (!is_swapped) {
|
136
|
+
|
137
|
+
break;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
int main() {
|
148
|
+
|
149
|
+
std::vector<int> v = {3, 1, 4, 5, 2};
|
150
|
+
|
151
|
+
BubbleSort(std::begin(v), std::end(v));
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
for (const auto &i : v) {
|
156
|
+
|
157
|
+
std::cout << i << ", ";
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
std::cout << "\n";
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```
|