Here's an implementation of binary search in Swift:
main.swift399 chars18 lines
To perform binary search, call this function passing it the array you want to search and the key you want to find. The function will return the index of the key in the array, or nil
if the key is not found.
Here's an example of how to use it:
main.swift175 chars7 lines
This will output "Found 5 at index 4".
gistlibby LogSnag