Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1317073

Browse files
wikipedia link
1 parent 2b95c9b commit 1317073

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎sorting/counting-sort.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
// Counting sort
1+
// https://en.wikipedia.org/wiki/Counting_sort
22

33
let countingSort = (arr) => {
44
let result = [],
5-
counting = [],
5+
count = [],
66
maxArr = Math.max(...arr)
77

88
for (let i = 0; i <= maxArr; i++) {
9-
counting[i] = 0
9+
count[i] = 0
1010
}
1111

1212
for (let j = 0; j < arr.length; j++) {
13-
counting[arr[j]] = counting[arr[j]] + 1
13+
count[arr[j]] = count[arr[j]] + 1
1414
}
1515

1616
for (let i = 1; i <= maxArr; i++) {
17-
counting[i] = counting[i] + counting[i - 1]
17+
count[i] = count[i] + count[i - 1]
1818
}
1919

2020
for (let j = arr.length - 1; j >= 0; j--) {
21-
counting[arr[j]] = counting[arr[j]] - 1
22-
result[counting[arr[j]]] = arr[j]
21+
count[arr[j]] = count[arr[j]] - 1
22+
result[count[arr[j]]] = arr[j]
2323
}
2424

2525
return result

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /