forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_interface.go
executable file
·35 lines (29 loc) · 925 Bytes
/
min_interface.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
// min_interface.go
package min
type Miner interface {
Len() int
ElemIx(ix int) interface{}
Less(i, j int) bool
Swap(i, j int)
}
func Min(data Miner) interface{} {
min := data.ElemIx(0)
for i := 1; i < data.Len(); i++ {
if data.Less(i, i-1) {
min = data.ElemIx(i)
} else {
data.Swap(i, i-1)
}
}
return min
}
type IntArray []int
func (p IntArray) Len() int { return len(p) }
func (p IntArray) ElemIx(ix int) interface{} { return p[ix] }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringArray []string
func (p StringArray) Len() int { return len(p) }
func (p StringArray) ElemIx(ix int) interface{} { return p[ix] }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }