Last active
July 15, 2017 18:58
-
-
Save gerep/a38c8a62faa0f84ec104bf2ca7e42a9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} | |
p := binarySearch(primes, 59) | |
fmt.Println("O número 11 esta na posição ", p) | |
} | |
func binarySearch(list []int, item int) int { | |
low := 0 | |
high := len(list) - 1 | |
for low <= high { | |
mid := (low + high) / 2 | |
guess := list[mid] | |
if guess == item { | |
return mid | |
} else if guess > item { | |
high = mid - 1 | |
} else { | |
low = mid + 1 | |
} | |
} | |
return 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
a := binarySearch(253991398, 91) | |
fmt.Println("Attempts:", a) | |
} | |
func binarySearch(numbers, item int) int { | |
high := numbers | |
low := 0 | |
attempts := 0 | |
for item <= numbers { | |
guess := (low + high) / 2 | |
if item == guess { | |
return attempts | |
} else if guess > item { | |
high = guess - 1 | |
} else { | |
low = guess + 1 | |
} | |
attempts++ | |
} | |
return 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
var attempts int | |
func main() { | |
primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 113, 145, 209, 278, 345, 567, 890, 123455, 12987312} | |
binarySearch(primes, 121, 0, len(primes)) | |
} | |
func binarySearch(l []int, i, low, high int) { | |
attempts++ | |
if high < low { | |
fmt.Printf("Could not find %d after %d attempts", i, attempts) | |
} | |
mid := low + ((high - low) / 2) | |
fmt.Println("mid", mid) | |
if l[mid] > i { | |
binarySearch(l, i, low, mid-1) | |
} else if l[mid] < i { | |
binarySearch(l, i, mid+1, high) | |
} else { | |
fmt.Printf("Found %d after %d attempts", mid, attempts) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
var attempts int | |
func main() { | |
primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 113, 145, 209, 278, 345, 567, 890, 123455, 12987312} | |
binarySearch(primes, 11) | |
} | |
func binarySearch(l []int, i int) { | |
attempts++ | |
if len(l) > 1 { | |
half := len(l) / 2 | |
guess := l[half] | |
if guess == i { | |
fmt.Printf("Found %d after %d attempts", i, attempts) | |
} else if guess < i { | |
binarySearch(l[half:], i) | |
} else if guess > i { | |
binarySearch(l[:half], i) | |
} | |
} else if l[0] == i { | |
fmt.Printf("Found %d after %d attempts", l[0], attempts) | |
} else { | |
fmt.Printf("Could not find %d after %d attempts", i, attempts) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment