计数排序----赠品1

解题思路:

例:

程序:

import time
import random

def cal_time(func):            # 装饰器 ,用来检测算法所执行的时间
    def wrapper(*args,**kwargs):
        t1=time.time()
        result=func(*args,**kwargs)
        t2=time.time()
        print("%s running time: %s secs." %(func.__name__,t2-t1))
        return result
    return wrapper

@cal_time
def count_sort(li,max_num):
    count = [0 for i in range(max_num + 1)] # 从 0 ~ max_num
    for num in li: # 遍历li
        count[num] += 1  #  若 li 数出现一次 , 那么就使索引的次数加1
    i = 0
    for num,m in enumerate(count): # enumerate :索引+值 num为索引序号 m 为个数
        for j in range(m):
            li[i] = num
            i += 1

data = []
for i in range(100000):
    data.append(random.randint(0,100))  # randint 前后都包括
count_sort(data,100)
print(data)

程序的运行结果为:

``````````````

 

posted @ 2018-06-24 09:21  追风的小蚂蚁  阅读(229)  评论(0编辑  收藏  举报