forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcffdac
commit b362685
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const countingSort = array => { | ||
if (array.length <= 1) return | ||
|
||
const max = findMaxValue(array) | ||
const counts = new Array(max + 1) | ||
|
||
// 计算每个元素的个数,放入到counts桶中 | ||
// counts下标是元素,值是元素个数 | ||
array.forEach(element => { | ||
if (!counts[element]) { | ||
counts[element] = 0 | ||
} | ||
counts[element]++ | ||
}) | ||
|
||
// counts下标是元素,值是元素个数 | ||
// 例如: array: [6, 4, 3, 1], counts: [empty, 1, empty, 1, 1, empty, 1] | ||
// i是元素, count是元素个数 | ||
let sortedIndex = 0 | ||
counts.forEach((count, i) => { | ||
while (count > 0) { | ||
array[sortedIndex] = i | ||
sortedIndex++ | ||
count-- | ||
} | ||
}) | ||
// return array | ||
} | ||
|
||
function findMaxValue(array) { | ||
let max = array[0] | ||
for (let i = 1; i < array.length; i++) { | ||
if (array[i] > max) { | ||
max = array[i] | ||
} | ||
} | ||
return max | ||
} |