-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.go
72 lines (67 loc) · 2.61 KB
/
binarysearch.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
package main
import (
"fmt"
// "math"
"sort"
)
var loopcnt int
var lower int
var upper int
func main() {
const searchpattern = "Peter A Wright/TITCHFIELD/ONS"
employee := []string{}
employee = append(employee, "TONY LIU/NEWPORT/ONS")
employee = append(employee, "Rachel Varney/TITCHFIELD/ONS")
employee = append(employee, "Madara Watkins/NEWPORT/ONS")
employee = append(employee, "Ian Cullen/NEWPORT/ONS")
employee = append(employee, "Tanya Vaughan/NEWPORT/ONS")
employee = append(employee, "Marianna Vasili/TITCHFIELD/ONS")
employee = append(employee, "Kate Vaughan Jones/NEWPORT/ONS")
employee = append(employee, "Cath Fawcett/NEWPORT/ONS")
employee = append(employee, "David J Williams/NEWPORT/ONS")
employee = append(employee, "Ben Whitestone/NEWPORT/ONS")
employee = append(employee, "Penny Witham/NEWPORT/ONS")
employee = append(employee, "George Zygmund/NEWPORT/ONS")
employee = append(employee, "Peter A Wright/TITCHFIELD/ONS")
employee = append(employee, "Lewis Hodge/NEWPORT/ONS")
employee = append(employee, "Sharne Bailey/NEWPORT/ONS")
employee = append(employee, "Nick Vaughan/NEWPORT/ONS")
employee = append(employee, "Mike Wendes/TITCHFIELD/ONS")
employee = append(employee, "Nikola Bowers/TITCHFIELD/ONS")
employee = append(employee, "Donna Wall/NEWPORT/ONS")
employee = append(employee, "Sreeram Vedagiri/Titchfield/ONS")
employee = append(employee, "Stacey Vaughan/NEWPORT/ONS")
employee = append(employee, "Stephen D King/TITCHFIELD/ONS")
employee = append(employee, "Julia Abbott/NEWPORT/ONS")
employee = append(employee, "Ruth Williams/NEWPORT/ONS")
employee = append(employee, "Aaron Aardvark/NEWPORT/ONS")
sort.Strings(employee)
split := len(employee) / 2
upper = len(employee)
lower = 0
chop(employee, split, searchpattern)
}
func chop(employee []string, split int, searchpattern string) bool {
loopcnt++
fmt.Printf("Split = %v, Upper = %v, Lower = %v\n", split, upper, lower)
if loopcnt > len(employee)/2 {
fmt.Printf("Search Pattern %v is not found in %v iterations. Split = %v\n", searchpattern, loopcnt, split)
} else if employee[split] > searchpattern {
if split < upper {
upper = split
}
// split = int(math.Floor(float64((upper - lower) / 2)))
split -= (upper - lower) / 2
split-- // Because array starts at zero and we would never get there otherwise
chop(employee, split, searchpattern)
} else if employee[split] < searchpattern {
if split > lower {
lower = split
}
split += (upper - lower) / 2
chop(employee, split, searchpattern)
} else {
fmt.Printf("Found = %v at array position %v in %v iterations\n", employee[split], split, loopcnt)
}
return true
}