         // Swap the pivot element with the first element that is greater than
         // the pivot. Now the pivot sits between the < and >= regions and the
         // array is properly partitioned.
-        (a[i], a[a.endIndex-1]) = (a[a.endIndex-1], a[i])
+        array.swapAt(i, hi)
         return i
-    }
-    
-    /*
-     takes an array and an index n and returns an array with the nth value being the nth largest
-     */
-    private static func quickSelect(n: Int, arr: [Element], kdDimension: Int) -> [Element] {
-        if arr.count == 1 {
-            return [arr.first!]
-        }
-        
-        var varr = arr
-        let selector = partitionLomuto(&varr[0...arr.endIndex-1], kdDimension: kdDimension)
-        
-        if selector == n {
-            return varr
-        }
-        else if n > selector {
-            let s = selector+1
-            return  Array(varr[...selector] + quickSelect(n: n, arr: varr[s...], kdDimension: kdDimension))
-        }
-        else {
-            return  Array(quickSelect(n: n, arr: varr[..<selector], kdDimension: kdDimension) + varr[selector...])
-        }
-    }
-    
-    private static func quickSelect(n: Int, arr: ArraySlice<Element>, kdDimension: Int) -> ArraySlice<Element> {
-        if arr.count == 1 {
-            return [arr.first!]
-        }
-        
-        var varr = arr
-        let selector = partitionLomuto(&varr, kdDimension: kdDimension)
-        
-        if selector == n {
-            return varr
-        }
-        else if n > selector {
-            let s = selector+1
-            return varr[..<s] + quickSelect(n: n, arr: varr[s...], kdDimension: kdDimension)
-        }
-        else {
-            return quickSelect(n: n, arr: varr[..<selector], kdDimension: kdDimension) + varr[selector...]
-        }
     }
     
     /// Returns `true` iff `self` is empty.

     if the pivot value occurs more than once, its duplicates will be found in the
     right partition.
     */
    private static func partitionLomuto(_ a: inout ArraySlice<Element>, kdDimension: Int) -> Int {
        // We always use the highest item as the pivot.
        let pivot = a.last!
        
        // This loop partitions the array into four (possibly empty) regions:
        //   [a.startIndex  ...   i] contains all values < pivot,
        //   [i+1  ...          j-1] contains all values >= pivot,
        //   [j    ..< a.endIndex-1] are values we haven't looked at yet,
        //   [a.endIndex           ] is the pivot value.
        var i = a.startIndex
        
        for j in a.startIndex..<a.endIndex-1 {
            if a[j].kdDimension(kdDimension) < pivot.kdDimension(kdDimension) {
                (a[i], a[j]) = (a[j], a[i])
                i += 1
            }
        }

        else {
            var median = values.count / 2
            
            let selected = KDTree.quickSelect(targetIndex: median, array: values[0..<values.count], kdDimension: currentSplittingDimension)
            let medianElement = selected[median]
            let medianValue = medianElement.kdDimension(currentSplittingDimension)
          
