arr-algo.go (584B)
1 package algos 2 3 import ( 4 "math/rand" 5 "time" 6 ) 7 8 // Fischer-Yates in-place shuffle. 9 func Shuffle(arr []string) { 10 rand.Seed(time.Now().UnixNano()) 11 arr_len := len(arr) 12 for i := arr_len - 1; i > 0; i-- { 13 j := rand.Intn(i + 1) 14 arr[j], arr[i] = arr[i], arr[j] 15 } 16 } 17 18 // Return index of element, or -1 if not found. 19 func BinarySearch(arr []string, val string) int { 20 low, high := 0, len(arr)-1 21 for low <= high { 22 mid := (low + high) >> 1 23 if arr[mid] == val { 24 return mid 25 } else { 26 if arr[mid] > val { 27 high = mid - 1 28 } else { 29 low = mid + 1 30 } 31 } 32 } 33 return -1 34 }