Created
May 11, 2017 08:27
-
-
Save chriswebb09/1688aa69ab84602acf42c13184e6cb96 to your computer and use it in GitHub Desktop.
Binary Search
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
let arrOne = [1, 2, 3, 4, 5, 6] | |
func binarySearch(_ inputArray: [Int], element: Int) -> Int? { | |
var begin = 0 | |
var end = inputArray.count | |
while begin < end { | |
let mid = begin + (end - begin) / 2 | |
if inputArray[mid] == element { | |
return mid | |
} else if inputArray[mid] < element { | |
begin = mid | |
} else { | |
end = mid | |
} | |
} | |
return nil | |
} | |
binarySearch([1, 2, 3, 4, 5], element: 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment