from itertools import chain def bucket_sort(a, bucket_count): min_value, max_value = min(a), max(a) # 桶大小 bucket_size = (max_value - min_value + 1) // bucket_count res = [[] for _ in range(bucket_count + 1)] for x in a: idx = (x - min_value) // bucket_size res[idx].append(x) # 每个桶单独排序,可以采用其他排序算法 for sublist in res: sublist.sort() return list(chain(*res)) # 读入一个整数n n = int(input()) a = list(map(int, input().split())) a = bucket_sort(a, min(n, 10000)) print(' '.join(map(str, a)))
【蓝桥杯】省赛无忧班(Python 组)第 2 期 9.5桶排序
2024-02-21 04:54:01 29 阅读