排序

桶排序

# coding:utf-8
import numpy as np
import bucket_sort
if __name__ == '__main__':

    # input original number
    ori_data = raw_input('please input numbers with blank seprated:')
    ori_data = ori_data.split(' ')
    num = len(ori_data)

    # transform ori_data from 'str' into 'int'
    for i in range(num):
        ori_data[i] = int(ori_data[i])


    # bucket_sort
    output = bucket_sort.bucket_sort(ori_data,num)
    print(output)
    print('Done')
# coding:utf-8
import numpy as np
# define bucket_sort
def bucket_sort(input_data,num):
    # 建立一个桶矩阵用来存储输入数据:
    bucket_matrix = [0] * (max(input_data)+1)

    # 将所有的数按顺序放在bucket矩阵中
    for i in input_data:
        bucket_matrix[i] = bucket_matrix[i]+1

    # 按照bucket的顺序将所有的数取出
    output = []
    for j in range(len(bucket_matrix)):
        if bucket_matrix[j]!=0:
            for k in range(bucket_matrix[j]):
                output = np.append(output,j)

    return output

你可能感兴趣的:(排序)