Skip to content

Instantly share code, notes, and snippets.

@stone3311
Created December 8, 2015 17:02
Show Gist options
  • Select an option

  • Save stone3311/95678112855dbe0e0d28 to your computer and use it in GitHub Desktop.

Select an option

Save stone3311/95678112855dbe0e0d28 to your computer and use it in GitHub Desktop.
This is a simple binary search algorithm implemented in Swift 2
func binarySearch(list: [Int], value: Int) -> Int {
let count = list.count
var min: Int = 0
var max: Int = count
var mid: Int = Int(round(Double(max / 2)))
while(list[mid] != value) {
if (list[mid] > value) {
max = mid
mid = Int(round(Double((max - min) / 2 + min)))
} else {
min = mid
mid = Int(round(Double((max - min) / 2 + min)))
}
}
return mid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment