mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-15 02:40:58 +08:00
@@ -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);
|
||||
|
||||
@@ -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 位執行計數排序
|
||||
|
||||
Reference in New Issue
Block a user