Skip to content

Commit 00f39c7

Browse files
goldSunshinehustcc
authored andcommitted
基数排序增加python实现 (#31)
* 增加python实现 * 基数排序格式调整
1 parent bee257b commit 00f39c7

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

Diff for: 10.radixSort.md

+36-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
基数排序有两种方法:
99

10-
这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异
10+
这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异案例看大家发的
1111

1212
- 基数排序:根据键值的每位数字来分配桶;
1313
- 计数排序:每个桶只存储单一键值;
@@ -49,7 +49,39 @@ function radixSort(arr, maxDigit) {
4949
}
5050
```
5151

52-
## 4. Java 代码实现
52+
53+
## 4. python 代码实现
54+
55+
```python
56+
def radix(arr):
57+
58+
digit = 0
59+
max_digit = 1
60+
max_value = max(arr)
61+
#找出列表中最大的位数
62+
while 10**max_digit < max_value:
63+
max_digit = max_digit + 1
64+
65+
while digit < max_digit:
66+
temp = [[] for i in range(10)]
67+
for i in arr:
68+
#求出每一个元素的个、十、百位的值
69+
t = int((i/10**digit)%10)
70+
temp[t].append(i)
71+
72+
coll = []
73+
for bucket in temp:
74+
for i in bucket:
75+
coll.append(i)
76+
77+
arr = coll
78+
digit = digit + 1
79+
80+
return arr
81+
```
82+
83+
84+
## 5. Java 代码实现
5385

5486
```java
5587
/**
@@ -134,7 +166,7 @@ public class RadixSort implements IArraySort {
134166
}
135167
```
136168

137-
## 5. PHP 代码实现
169+
## 6. PHP 代码实现
138170

139171
```php
140172
function radixSort($arr, $maxDigit = null)
@@ -169,4 +201,4 @@ function radixSort($arr, $maxDigit = null)
169201

170202
return $arr;
171203
}
172-
```
204+
```

0 commit comments

Comments
 (0)