1
+ // radixsort.go
2
+ // description: Implementation of in-place radixsort algorithm
3
+ // details:
4
+ // A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Radix_sort)
5
+
1
6
package sort
2
7
3
- import "github.com/TheAlgorithms/Go/math/max"
8
+ import (
9
+ "github.com/TheAlgorithms/Go/constraints"
10
+ "github.com/TheAlgorithms/Go/math/max"
11
+ )
4
12
5
- func countSort (arr []int , exp int ) []int {
13
+ func countSort [ T constraints. Integer ] (arr []T , exp T ) []T {
6
14
var digits [10 ]int
7
- var output = make ([]int , len (arr ))
15
+ var output = make ([]T , len (arr ))
8
16
9
17
for _ , item := range arr {
10
18
digits [(item / exp )% 10 ]++
@@ -21,22 +29,22 @@ func countSort(arr []int, exp int) []int {
21
29
return output
22
30
}
23
31
24
- func unsignedRadixSort (arr []int ) []int {
32
+ func unsignedRadixSort [ T constraints. Integer ] (arr []T ) []T {
25
33
if len (arr ) == 0 {
26
34
return arr
27
35
}
28
36
maxElement := max .Int (arr ... )
29
- for exp := 1 ; maxElement / exp > 0 ; exp *= 10 {
37
+ for exp := T ( 1 ) ; maxElement / exp > 0 ; exp *= 10 {
30
38
arr = countSort (arr , exp )
31
39
}
32
40
return arr
33
41
}
34
42
35
- func RadixSort (arr []int ) []int {
43
+ func RadixSort [ T constraints. Integer ] (arr []T ) []T {
36
44
if len (arr ) < 1 {
37
45
return arr
38
46
}
39
- var negatives , nonNegatives []int
47
+ var negatives , nonNegatives []T
40
48
41
49
for _ , item := range arr {
42
50
if item < 0 {
0 commit comments