1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| def bucketSort(nums, n): if max(nums) > len(nums): n = max(nums) else: n = len(nums) + 1 mid = max(nums) // n if mid == 0: mid = 1 bucket = [[] for i in range(n + 1)] for i in nums: bucket[int(i / mid)].append(i) print(bucket) sort_nums = [] for j in range(len(bucket)): if len(bucket[j]) != 0: if len(bucket[j]) >= 2 and len(set(nums)) != 1: bucket[j] = bucketSort(bucket[j], n) for y in bucket[j]: sort_nums.append(y) return sort_nums
nums = [5, 6, 3, 2, 1, 2, 0, 8, 0, 65] print(bucketSort(nums, 5))
|