Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.39 KB | None | 0 0
  1. func binarySearch<T: Comparable>(xs: [T], x: T) -> Int? {
  2.   var recurse: ((Int, Int) -> Int?)!
  3.   recurse = {(low, high) in switch (low + high) / 2 {
  4.     case _ where high < low: return nil
  5.     case let mid where xs[mid] > x: return recurse(low, mid - 1)
  6.     case let mid where xs[mid] < x: return recurse(mid + 1, high)
  7.     case let mid: return mid
  8.   }}
  9.   return recurse(0, xs.count - 1)
  10. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement