-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_5.go
126 lines (99 loc) · 1.86 KB
/
3_5.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
// バブルソート、挿入ソート、選択ソートの実装
func bubbleSort(a []int, b []string, n int) ([]int, []string) {
flag := 1
for flag > 0 {
flag = 0
for i := n - 1; i > 0; i-- {
if a[i - 1] > a[i] {
tmp := a[i - 1]
a[i - 1] = a[i]
a[i] = tmp
tmp_b := b[i - 1]
b[i - 1] = b[i]
b[i] = tmp_b
flag = 1
}
}
}
return a, b
}
func selectionSort(a []int, b []string, n int) ([]int, []string) {
for i := 0; i < n; i++ {
min_j := i
for j := i; j < n; j++ {
if a[j] < a[min_j] {
min_j = j
}
}
tmp := a[i]
a[i] = a[min_j]
a[min_j] = tmp
tmp_b := b[i]
b[i] = b[min_j]
b[min_j] = tmp_b
}
return a, b
}
func insertSort(a []int, b []string, n int) ([]int, []string) {
for i := 1; i < n; i++ {
tmp_v := a[i]
tmp_b_v := b[i]
for j := i; j > 0; j-- {
if a[j - 1] < tmp_v {
a[j] = tmp_v
b[j] = tmp_b_v
break
}
a[j] = a[j - 1]
b[j] = b[j - 1]
}
}
return a, b
}
func returnInputValueSlice(scn *bufio.Scanner, n int) ([]int, []string) {
var a []int
var b []string
for i := 0; i < n; i++ {
scn.Scan()
text := scn.Text()
b = append(b, text)
num, _ := strconv.Atoi(text[1:])
a = append(a, num)
}
return a, b
}
func output(a []int, b[]string) {
for i := range a {
print(fmt.Sprintf("%v ", a[i]))
}
for i := range b {
print(fmt.Sprintf("%v ", b[i]))
}
}
func main() {
var n int
var a []int
var b []string
fmt.Scan(&n)
scn := bufio.NewScanner(os.Stdin)
scn.Split(bufio.ScanWords)
a, b = returnInputValueSlice(scn, n)
a_bu, b_bu := bubbleSort(a, b, n)
print("bubble sort\n")
output(a_bu, b_bu)
print("\n")
a_se, b_se := selectionSort(a, b, n)
print("selection sort\n")
output(a_se, b_se)
print("\n")
a_in, b_in := insertSort(a, b, n)
print("insert sort\n")
output(a_in, b_in)
}