How do I insert an element at the correct position into a sorted array in Swift?
Here is a possible implementation in Swift using binary search (from http://rosettacode.org/wiki/Binary_search#Swift with slight modifications): extension Array { func insertionIndexOf(_ elem: Element, isOrderedBefore: (Element, Element) -> Bool) -> Int { var lo = 0 var hi = self.count – 1 while lo <= hi { let mid = (lo + hi)/2 if isOrderedBefore(self[mid], elem) { … Read more