脑袋突然开光, 连忙打开电脑
想到一个之前看短视频刷到的各种算法效率对比,看到一种非常''高效''的随机算法,于是打算不看教程,直接开写,pycharm激活码用不了了
所以只能用只有大佬采用的vim写了,好在算法代码一般不太多,只有区区20+ 行,但算法只有6行.......
<code>
from time import time
from random import randint
a = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
n = 0
be = time()
while True:
if a == sorted(a):
break
i = randint(0, len(a) - 1)
j = randint(0, len(a) - 1)
a[i], a[j] = a[j], a[i]
n += 1
print(a)
af = time()
al = (af - be) * 1000
print('Finished.')
print(f'It only spent {al}ms.')
print(f'There were {len(a)} numbers and it only tried {n} times.')
print('This is random sort.')
print('It\'s really efficient.')
</code>
非常简单,也非常高效(仅在写代码过程中高效...)
下面是实例:
可以看到,以倒序排列的1~10只用了1431601ms,也就是23min,只尝试了6872947次就排好了,非常高效(doge)
I use arch,btw