-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbogo_sort.cpp
More file actions
47 lines (41 loc) · 987 Bytes
/
Copy pathbogo_sort.cpp
File metadata and controls
47 lines (41 loc) · 987 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
//
// ボゴソート
//
// cf.
// ソートを極める! 〜 なぜソートを学ぶのか 〜
// https://qiita.com/drken/items/44c60118ab3703f7727f
//
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
void Shuffle(vector<int> &a) {
int n = (int)a.size();
for (int i = n - 1; i > 0; --i) {
int k = rand() % (i + 1);
swap(a[i], a[k]);
}
}
void BogoSort(vector<int> &a) {
while (true) {
bool swapped = true;
for (int i = 0; i + 1 < (int)a.size(); ++i) {
if (a[i] > a[i + 1]) {
swapped = false;
}
}
if (swapped) break;
Shuffle(a);
}
}
int main() {
int n; // 要素数
cin >> n;
vector<int> a(n); // 整列したい配列ベクトル (サイズ を n に初期化)
for (int i = 0; i < n; ++i) {
cin >> a[i]; // 整列したい配列を取得
}
/* ボゴソート */
BogoSort(a);
return 0;
}