Monkey's Sort 排序算法
hame
·
·
个人记录
1. 对 $x$ 到 $n$ 之间的数采用 $\text{random shuffle}$ 随机化。( $x-1=\operatorname{rand}() \bmod n$ )
1. 检查是否有序。
1. 若不有序,返回操作 $1$ 。若有序,跳出程序,排序完成。
此程序的时间复杂度为 $O(n \times 玄学)$ ,十分优秀,能得到 [20](https://www.luogu.com.cn/record/215413249) 分的好成绩!
代码如下:
```c
#include<bits/stdc++.h>
#define r(k) rand()%k+1
using namespace std;
int n,ans,a[200501],x,op=1;
int main()
{
srand(time(0));
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
while(op)
{
for(int i=2;i<=n&&op;i++) if(a[i]<a[i-1]) op=0;
op=(op+1)%2;
if(op) random_shuffle(a+1+rand()%n,a+1+n);
}
for(int i=1;i<=n;i++) cout<<a[i]<<" ";
return 0;
}
```