-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloatSearch.go
66 lines (54 loc) · 1.05 KB
/
floatSearch.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
package main
import (
"fmt"
)
func insertFloat(l []float64, f float64) []float64 {
var i int
if len(l) == 0 {
return []float64{f}
}
if len(l) == 1 {
if f < l[0] {
return []float64{f,l[0]}
} else {
return append(l, f)
}
}
low := 0
high := len(l) - 1
for low+1 != high {
i = ((high - low)/2) + low
fmt.Println(low, high, i, f, l[i])
if f < l[i] {
high = i
} else {
low = i
}
}
fmt.Println(low, high, i, f, l[i])
switch {
case f < l[low]:
fmt.Println(f, l[low])
l = append([]float64{f}, l...)
case f > l[high]:
fmt.Println(l[high], f)
l = append(l,f)
default:
fmt.Println(l[low], f, l[high])
i = low+1
l = append(l, 0) // add a blank element to end of slice
copy(l[i+1:], l[i:]) // shift from i+1 to the right
l[i] = f // insert f
}
return l
}
func main() {
f := make([]float64, 100, 100)
for i:=0; i<100; i++ {
f[i] = float64(i)/100.0
}
fmt.Println(insertFloat(f, 0.355))
fmt.Println(insertFloat(f, 10.0))
fmt.Println(insertFloat(f, -1.0))
fmt.Println(insertFloat(f, .989))
}