用python代码分别实现计数排序、基排序和桶排序算法
时间: 2023-06-13 15:05:15 浏览: 101
计数排序:
```python
def counting_sort(arr):
max_num = max(arr)
count = [0] * (max_num + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(max_num + 1):
sorted_arr.extend([i] * count[i])
return sorted_arr
```
基排序:
```python
def radix_sort(arr):
max_num = max(arr)
exp = 1
while max_num // exp > 0:
buckets = [[] for _ in range(10)]
for num in arr:
buckets[(num // exp) % 10].append(num)
arr = []
for bucket in buckets:
arr.extend(bucket)
exp *= 10
return arr
```
桶排序:
```python
def bucket_sort(arr):
buckets = [[] for _ in range(10)]
for num in arr:
buckets[num // 10].append(num)
sorted_arr = []
for bucket in buckets:
sorted_arr.extend(sorted(bucket))
return sorted_arr
```
阅读全文