From 7e904c8ff685ebbdf76e9a3b4cdbc4b40bb654a8 Mon Sep 17 00:00:00 2001 From: Red Wood <58332748+prinpal@users.noreply.github.com> Date: Sun, 9 Mar 2025 06:39:32 +0800 Subject: [PATCH] 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. --- codes/python/chapter_sorting/counting_sort.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/codes/python/chapter_sorting/counting_sort.py b/codes/python/chapter_sorting/counting_sort.py index 7cac3cd22..bfa33e2f9 100644 --- a/codes/python/chapter_sorting/counting_sort.py +++ b/codes/python/chapter_sorting/counting_sort.py @@ -9,9 +9,7 @@ def counting_sort_naive(nums: list[int]): """计数排序""" # 简单实现,无法用于排序对象 # 1. 统计数组最大元素 m - m = 0 - for num in nums: - m = max(m, num) + m = max(nums) # 2. 统计各数字的出现次数 # counter[num] 代表 num 的出现次数 counter = [0] * (m + 1)