forked from brianaeng/data-structure-implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.go
59 lines (49 loc) · 1.46 KB
/
extra.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
// Write a function to print all integer values in a binary tree in breadth first traversal iteratively. (Hint: Use a Queue.)
// Write a function to print all integer values in a binary tree in pre-order traversal iteratively. (Hint: Use a Stack.)
// How would you implement a Queue using 2 Stacks?
// How would you implement a Stack using 2 Queues?
package main
import (
"fmt"
)
//Search a sorted array for a value
// func RecursiveBinarySearch(value int, arr []int) bool {
// currentIndex := (len(arr) -1)/2 //
// currentItem := arr[currentIndex]
// subArray := arr
// if currentItem == value {
// //Return its place
// return true
// } else if currentItem > value {
// subArray = subArray[:currentIndex]
// fmt.Println("in here is num is bigger than value")
// fmt.Println(subArray)
// RecursiveBinarySearch(value, subArray)
// } else if currentItem < value {
// subArray = subArray[currentIndex:]
// fmt.Println("in here is num is smaller than value")
// fmt.Println(subArray)
// RecursiveBinarySearch(value, subArray)
// }
// return false
// }
func IterativeBinarySearch(value int, arr []int) int {
low := 0
high := len(arr) - 1
for low <= high {
mid := (high + low) / 2
if arr[mid] == value {
return mid
} else if arr[mid] < value {
low = mid + 1
} else if arr[mid] > value {
high = mid - 1
}
}
return -1
}
func main() {
startArray := []int{1,2,3,4,5,6}
result := IterativeBinarySearch(5, startArray)
fmt.Println(result)
}