From 09f7223067b5dfd32db1732ce7d600e5278b009f Mon Sep 17 00:00:00 2001 From: Asashishi_Nyan! Date: Sat, 17 May 2025 17:17:22 +0800 Subject: [PATCH] optimized the js api use of counting_sort chapter for Math.max() (#1748) * Update counting_sort.ts optimized the Math.max * Update counting_sort.ts * Update counting_sort.js * Update radix_sort.ts * Update radix_sort.js --- codes/javascript/chapter_sorting/counting_sort.js | 10 ++-------- codes/javascript/chapter_sorting/radix_sort.js | 7 +------ codes/typescript/chapter_sorting/counting_sort.ts | 10 ++-------- codes/typescript/chapter_sorting/radix_sort.ts | 7 +------ 4 files changed, 6 insertions(+), 28 deletions(-) diff --git a/codes/javascript/chapter_sorting/counting_sort.js b/codes/javascript/chapter_sorting/counting_sort.js index dee3c1603..96fbcefc2 100644 --- a/codes/javascript/chapter_sorting/counting_sort.js +++ b/codes/javascript/chapter_sorting/counting_sort.js @@ -8,10 +8,7 @@ // 简单实现,无法用于排序对象 function countingSortNaive(nums) { // 1. 统计数组最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m = Math.max(...nums); // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 const counter = new Array(m + 1).fill(0); @@ -31,10 +28,7 @@ function countingSortNaive(nums) { // 完整实现,可排序对象,并且是稳定排序 function countingSort(nums) { // 1. 统计数组最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m = Math.max(...nums); // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 const counter = new Array(m + 1).fill(0); diff --git a/codes/javascript/chapter_sorting/radix_sort.js b/codes/javascript/chapter_sorting/radix_sort.js index 7d9627db9..11f882ee8 100644 --- a/codes/javascript/chapter_sorting/radix_sort.js +++ b/codes/javascript/chapter_sorting/radix_sort.js @@ -41,12 +41,7 @@ function countingSortDigit(nums, exp) { /* 基数排序 */ function radixSort(nums) { // 获取数组的最大元素,用于判断最大位数 - let m = Number.MIN_VALUE; - for (const num of nums) { - if (num > m) { - m = num; - } - } + let m = Math.max(... nums); // 按照从低位到高位的顺序遍历 for (let exp = 1; exp <= m; exp *= 10) { // 对数组元素的第 k 位执行计数排序 diff --git a/codes/typescript/chapter_sorting/counting_sort.ts b/codes/typescript/chapter_sorting/counting_sort.ts index cdf60c0a4..2422c75de 100644 --- a/codes/typescript/chapter_sorting/counting_sort.ts +++ b/codes/typescript/chapter_sorting/counting_sort.ts @@ -8,10 +8,7 @@ // 简单实现,无法用于排序对象 function countingSortNaive(nums: number[]): void { // 1. 统计数组最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m: number = Math.max(...nums); // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 const counter: number[] = new Array(m + 1).fill(0); @@ -31,10 +28,7 @@ function countingSortNaive(nums: number[]): void { // 完整实现,可排序对象,并且是稳定排序 function countingSort(nums: number[]): void { // 1. 统计数组最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m: number = Math.max(...nums); // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 const counter: number[] = new Array(m + 1).fill(0); diff --git a/codes/typescript/chapter_sorting/radix_sort.ts b/codes/typescript/chapter_sorting/radix_sort.ts index 109a1e59f..40be727f3 100644 --- a/codes/typescript/chapter_sorting/radix_sort.ts +++ b/codes/typescript/chapter_sorting/radix_sort.ts @@ -41,12 +41,7 @@ function countingSortDigit(nums: number[], exp: number): void { /* 基数排序 */ function radixSort(nums: number[]): void { // 获取数组的最大元素,用于判断最大位数 - let m = Number.MIN_VALUE; - for (const num of nums) { - if (num > m) { - m = num; - } - } + let m: number = Math.max(... nums); // 按照从低位到高位的顺序遍历 for (let exp = 1; exp <= m; exp *= 10) { // 对数组元素的第 k 位执行计数排序