-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtask7.go
More file actions
77 lines (66 loc) · 1.25 KB
/
task7.go
File metadata and controls
77 lines (66 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package module2
import (
"bufio"
"fmt"
"os"
"strconv"
)
func printArray(arr []string) {
n := len(arr)
if n == 0 {
fmt.Println("empty")
} else {
for i := 0; i < n-1; i++ {
fmt.Printf("%s, ", arr[i])
}
if n > 0 {
fmt.Println(arr[n-1])
}
}
}
func radixSort(arr []string) []string {
maxArr := 0
for _, s := range arr {
if len(s) > maxArr {
maxArr = len(s)
}
}
for i := maxArr - 1; i >= 0; i-- {
buckets := make([][]string, 10)
for _, s := range arr {
if len(s) <= i {
buckets[0] = append(buckets[0], s)
} else {
a, _ := strconv.Atoi(string(s[i]))
buckets[a] = append(buckets[a], s)
}
}
arr = []string{}
for _, bucket := range buckets {
arr = append(arr, bucket...)
}
fmt.Println("**********")
fmt.Printf("Phase %d\n", maxArr-i)
for j, bucket := range buckets {
fmt.Printf("Bucket %d: ", j)
printArray(bucket)
}
}
return arr
}
func Task7() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
n, _ := strconv.Atoi(scanner.Text())
arr := make([]string, n)
for i := 0; i < n; i++ {
scanner.Scan()
arr[i] = scanner.Text()
}
fmt.Println("Initial array:")
printArray(arr)
sortedArr := radixSort(arr)
fmt.Println("**********")
fmt.Println("Sorted array:")
printArray(sortedArr)
}