首页 > 技术文章 > 一些奇怪的板子

hzf29721 2018-11-25 18:57 原文

1、猴子排序(Monkey_Sort)

号称最优复杂度最低的排序算法

最快复杂度:\(O(n)\)
最慢复杂度:\(O(+\infty)\)
平均复杂度:\(O(n\times n!)\)

算法思路:
1、检查数据是否有序,若有序,goto 3;若无序,goto 2
2、随机打乱数据,goto 1
3、输出数据

代码:

struct Monkey_Sort{
	inline bool check(int *st,int *ed){
		int *now=st;
		while(now+1!=ed){
			if(*(now+1)<*now)
				return 0;
			now++;
		}
		return 1;
	}
	inline void sort(int *st,int *ed){
		while(!check(st,ed))
			random_shuffle(st,ed);
	}
}M;

推荐阅读