     /// right partition.
     ///
     /// - Parameters:
-    ///   - array: the inout array slice
+    ///   - values: the pointer to the values
     ///   - kdDimension: the dimension sorted over
-    /// - Returns: the index of the pivot element in the new array
-    private static func partitionHoare(_ array: inout ArraySlice<Element>, kdDimension: Int) -> Int {
-        let lo = array.startIndex
-        let hi = array.endIndex-1
+    /// - Returns: the index of the pivot element in the pointer
+    private static func partitionHoare(_ values: UnsafeMutablePointer<Element>, startIndex lo: Int, endIndex: Int, kdDimension: Int) -> Int {
+        let hi = endIndex - 1
         guard lo < hi else { return lo }
         
         let randomIndex = Int.random(in: lo...hi)
-        array.swapAt(hi, randomIndex)
-        let pivot = array[hi]
+        values.swapAt(hi, randomIndex)
+        
+        let kdDimensionOfPivot = values[hi].kdDimension(kdDimension)
         
         // This loop partitions the array into four (possibly empty) regions:
         //   [lo   ...    i] contains all values < pivot,

        var j = hi - 1
        
        while true {
            while array[i].kdDimension(kdDimension) < pivot.kdDimension(kdDimension) {
                i += 1
            }
            while lo < j && array[j].kdDimension(kdDimension) >= pivot.kdDimension(kdDimension) {
                j -= 1
            }
            guard i < j else {
                break
            }
            array.swapAt(i, j)
        }
        
        // Swap the pivot element with the first element that is >= 
        // the pivot. Now the pivot sits between the < and >= regions and the
        // array is properly partitioned.
        array.swapAt(i, hi)
        return i
    }
    

    indirect case node(left: KDTree<Element>, value: Element, dimension: Int, right: KDTree<Element>)
}

fileprivate extension UnsafeMutablePointer {
    @_transparent
    func swapAt(_ i: Int, _ j: Int) {
        let temp = self[i]
        self[i] = self[j]
        self[j] = temp
    }
}

extension KDTree {
    public init(values: [Element], depth: Int = 0) {
        guard !values.isEmpty else {
