Created
March 3, 2017 03:59
-
-
Save tangingw/1f60cdc8dda18fe4a39c03ac8d42d531 to your computer and use it in GitHub Desktop.
This is golang binary search in recursive form
This file contains hidden or 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 | |
//Recursive Binary Search | |
func binarySearch(numList []int64, key int64) int { | |
low := 0 | |
high := len(numList) - 1 | |
if low <= high { | |
mid := ((high + low) / 2) | |
if numList[mid] > key { | |
return binarySearch(numList[:mid], key) | |
} else if numList[mid] < key { | |
return binarySearch(numList[mid+1:], key) | |
} else { | |
return 1 | |
} | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment