算法4-5.1.1键索引计数法

test_case =[('Anderson',2),('Brown',3),('Davis',3),('Garcia',4),('Harris',1),('Jackson',3),('Johnson',4),
            ('Jones',3),('Martin',1),('Martinez',2),('Miller',2),('Moore',1),('Robinson',2),('Smith',4),
            ('Taylor',3),('Thomas',4),('Thompson',4),('White',2),('Williams',3),('Wilson',4)]
class Student():
    def __init__(self,name,key):
        self.name = name
        self.key = key

a=[]
for name,key in test_case:
    a.append(Student(name,key))

count =[0 for i in range(6)]

# 计算出现频率
for i in range(len(a)):
    count[a[i].key+1]+=1

# 将频率转换位起始索引
for i in range(len(count)-1):
    count[i+1]+=count[i]

# 数据分类
aux = [0 for i in range(len(a))]
for i in range(len(a)):
    aux[count[a[i].key]] = a[i]
    count[a[i].key] += 1

for st in aux:
    print(st.name,st.key)

posted on 2019-03-10 13:45  Frank_Allen  阅读(142)  评论(0编辑  收藏  举报

导航