Skip to content

Commit 4b2c21f

Browse files
authored
Add Python
1 parent ae6fad9 commit 4b2c21f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

8.countingSort.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,23 @@ function countingSort(arr, maxValue) {
3232

3333
return arr;
3434
}
35-
```
35+
```
36+
## 3. JavaScript 代码实现
37+
38+
```python
39+
def countingSort(arr, maxValue):
40+
bucketLen = maxValue+1
41+
bucket = [0]*bucketLen
42+
sortedIndex =0
43+
arrLen = len(arr)
44+
for i in range(arrLen):
45+
if not bucket[arr[i]]:
46+
bucket[arr[i]]=0
47+
bucket[arr[i]]+=1
48+
for j in range(bucketLen):
49+
while bucket[j]>0:
50+
arr[sortedIndex] = j
51+
sortedIndex+=1
52+
bucket[j]-=1
53+
return arr
54+
```

0 commit comments

Comments
 (0)