今天看到一个非常玄奇的排序方法,大致上类似于桶排序,但是不是特别可靠,就是睡眠排序法,额,
简单的代码如下:
from time import sleep
items = [2, 4, 10, 2, 1, 7]
# 睡眠排序算法
def sleep_sort(i):
sleep(i * 0.01)
print(i)
import threading
[threading.Thread(target=sleep_sort, args=(i,)).start() for i in items]
复杂的代码如下(抄别人的)
import time
import random
from threading import Thread
from multiprocessing import Process
def sleepsort(n):
time.sleep(n*0.1)
print (n,"--",)
def thread_process_job(n, Thread_Process, job, value):
local_time=time.time()
threads_or_processes = [Thread_Process(target=job,args=(value[i],)) for i in range(n)]
for t in threads_or_processes:
t.start()
for t in threads_or_processes:
t.join()
print (n,Thread_Process.__name__," run job need ",time.time()-local_time)
if __name__=="__main__":
sort_list = [random.randint(1,40) for i in range(10)]
print ("Multi Threads")
for i in range(5):
sort_list = [random.randint(1,40) for i in range(10)]
thread_process_job(len(sort_list),Thread, sleepsort, sort_list)
print ("Multi Process")
for i in range(5):
sort_list = [random.randint(1,40) for i in range(10)]
thread_process_job(len(sort_list),Process, sleepsort,sort_list)
哈哈哈~~~