1. 程式人生 > >算法4-5.1.1鍵索引計數法

算法4-5.1.1鍵索引計數法

one sel tin jackson har ins 5.1 索引 row

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)

算法4-5.1.1鍵索引計數法