Skip to content

Commit 7e904c8

Browse files
authored
Update counting_sort.py (#1677)
Since the max function is called to find the maximum value, it doesn't seem necessary to traverse the list and call the max function several times to select the larger value.
1 parent 82fa8cb commit 7e904c8

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

codes/python/chapter_sorting/counting_sort.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def counting_sort_naive(nums: list[int]):
99
"""计数排序"""
1010
# 简单实现,无法用于排序对象
1111
# 1. 统计数组最大元素 m
12-
m = 0
13-
for num in nums:
14-
m = max(m, num)
12+
m = max(nums)
1513
# 2. 统计各数字的出现次数
1614
# counter[num] 代表 num 的出现次数
1715
counter = [0] * (m + 1)

0 commit comments

Comments
 (0)