File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ package sorting
2
+
3
+ import "fmt"
4
+
5
+ func countSort (arr []int , place int ) {
6
+ n := len (arr )
7
+ freq := make ([]int , 10 )
8
+ output := make ([]int , n )
9
+
10
+ // Finding freq of each element in the array
11
+ for i := 0 ; i < n ; i ++ {
12
+ freq [(arr [i ]/ place )% 10 ]++
13
+ }
14
+
15
+ // Calculating the place of each element in the output array
16
+ for i := 1 ; i < 10 ; i ++ {
17
+ freq [i ] += freq [i - 1 ]
18
+ }
19
+
20
+ // Building output array
21
+ for i := n - 1 ; i >= 0 ; i -- {
22
+ output [freq [(arr [i ]/ place )% 10 ]- 1 ] = arr [i ]
23
+ freq [(arr [i ]/ place )% 10 ]--
24
+ }
25
+
26
+ //Filling the array
27
+ for i := 0 ; i < n ; i ++ {
28
+ arr [i ] = output [i ]
29
+ fmt .Printf ("%d " , arr [i ])
30
+ }
31
+ }
32
+
33
+ func radixSort (arr []int ) {
34
+ max := findMax (arr )
35
+ place := 1
36
+ for max > 0 {
37
+ countSort (arr , place )
38
+ fmt .Println ()
39
+ max /= place
40
+ place *= 10
41
+ }
42
+ }
43
+
44
+ func findMax (arr []int ) int {
45
+ max := arr [0 ]
46
+ for i := 1 ; i < len (arr ); i ++ {
47
+ if arr [i ] > max {
48
+ max = arr [i ]
49
+ }
50
+ }
51
+ return max
52
+ }
You can’t perform that action at this time.
0 commit comments