Skip to content

Commit

Permalink
Merge pull request wangzheng0822#87 from jerryderry/counting-sort-python
Browse files Browse the repository at this point in the history
Counting sort python
  • Loading branch information
wangzheng0822 committed Oct 23, 2018
2 parents b97510d + a0267bf commit 5d400b7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python/14_sorts/counting_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
计数排序
Author: Wenru
"""

from typing import List
import itertools

def counting_sort(a: List[int]):
if len(a) <= 1: return

# a中有counts[i]个数不大于i
counts = [0] * (max(a) + 1)
for num in a:
counts[num] += 1
counts = list(itertools.accumulate(counts))

# 临时数组,储存排序之后的结果
a_sorted = [0] * len(a)
for num in reversed(a):
index = counts[num] - 1
a_sorted[index] = num
counts[num] -= 1

a = a_sorted


if __name__ == "__main__":
a1 = [1, 2, 3, 4]
counting_sort(a1)
print(a1)

a2 = [1, 1, 1, 1]
counting_sort(a2)
print(a2)

a3 = [4, 5, 0, 9, 3, 3, 1, 9, 8, 7]
counting_sort(a3)
print(a3)

0 comments on commit 5d400b7

Please sign in to comment.