python实现睡眠排序(sleep sort)

本文主要介绍使用python实现睡眠排序(Sleep Sort)。

import _thread
from time import sleep
import numpy as np

items = [2, 4, 10, 2, 1, 7]
# 求平均值
m = np.mean(items)
# 求数字位数
length = len(str(m).split('.')[0])
# 计算等待时间的比例
p = np.power(0.1, length -1)

# 睡眠排序算法
def sleep_sort(i):
    sleep(i * p)
    print(i)

# 测试:方案1
[_thread.start_new_thread(sleep_sort, (i,)) for i in items]

import threading
# 测试:方案2
[threading.Thread(target=sleep_sort, args=(i,)).start() for i in items]  

结果:

[17216, 15444, 15640, 15580, 13060, 16288]
1
2
2
4
7
[None, None, None, None, None, None]
1
2
2
4
7
10

猜你喜欢

转载自blog.csdn.net/github_39611196/article/details/84074401