Skip to content

Commit cd4629e

Browse files
authored
Merge pull request #121 from code4EE/my-sort
added counting sort
2 parents 39788aa + 44e0f61 commit cd4629e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sorts/counting_sort.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sorts
2+
3+
func CountingSort(arr []int, max int) []int {
4+
//use the max value to create a slice of item counts
5+
counts := make([]int, max+1)
6+
result := make([]int, 0)
7+
for _, item := range arr {
8+
counts[item]++
9+
}
10+
for i := 0; i < len(counts); i++ {
11+
count := counts[i]
12+
for j := 0; j < count; j++ {
13+
result = append(result, i)
14+
}
15+
}
16+
return result
17+
}

0 commit comments

Comments
 (0)