Happy googling — coding — Swift iOS

Marijan Vukcevich
Aug 24, 2017 · 2 min read

It seems that articles, books about algorithms are written everywhere for swift iOS. There is a lot of time saving solutions on stack exchange and others.

Also people do a lot of “happy googling” trying to learn thing or two and that’s fine and nice but it’s always important to understand the code that’s offered.

You have to understand it and test it and retest it and even break it to see how it behaves in different situations. It’s not the first time that someone stumbles on non working code. It’s normal.

The issue: the code comes from ‘book’, it should work perfectly, it’s been reviewed, published, and etc.

Logarithmic Time — O(log n)

//the binary approach - O(log n) logarithmic time
func binarySearch(key: Int, imin: Int, imax: Int) {

//find the value at the middle index
var midIndex : Double = round(Double((imin + imax) / 2))
var midNumber = numberList[Int(midIndex)]

//use recursion to reduce the possible search range
if (midNumber > key ) {
binarySearch(key, imin, Int(midIndex) - 1)
}
else if (midNumber < key ) {
binarySearch(key, Int(midIndex) + 1, imax)
}
else {
let results = "value \(key) found.."
}
} //end func

Also, the graph is included and short explanation.

The ‘problem’ is if you use it on face value without thinking is it working. It works but…..

Hey, run it in playground with this input:

binarySearch(key: 5, imin: 6, imax: 10)

It will search/loop +30,000 times and crash playground. *****error: Execution was interrupted.

On other hand, if you search some Recursive Functions Swift you might stumble on this:

8.6 Binary Search function:

//The Binary Search
func binarySearch(_ key:Int,_ numbers:[Int],left:Int = 0,right:Int = -1) -> Bool {
var right = right
if right == -1 {
right = numbers.count - 1
}
if left < right {
let mid = (left + right) / 2
if key < numbers[mid] {
return binarySearch(key, numbers, left: left, right: mid)
}
else if key > numbers[mid] {
return binarySearch(key, numbers, left: mid + 1, right: right)
} else {
return true
}
} else {
return numbers[left] == key
}
}

run it in Xcode playground with same inputs:

binarySearch(5, ar, left: 6, right: 10)

It will loop only 3 times, no crash. It works fine! That’s a huge difference.


Also I stumbled on some other site giving an example for recursive function but without small detail about recursive function:

Things to remember about recursion:- you can call a function inside of itself- you always have at least one base case in order to prevent the function calling itself infinite times!!!

Sometimes these small details are very obvious for some writers that in the heap of writing you forget to mention it.

Test, test, test and unit test again every code!

Happy ‘Googling’!

)
Marijan Vukcevich

Written by

iOS Development, Swift, Objective-C, usability (UX) and so on

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade