funcfindKthLargest(_nums: [Int], _k: Int) -> Int { var numbers = nums let targetIndex = numbers.count - k var left =0 var right = numbers.count -1 whiletrue { let index = patitation(&numbers, left, right) if index == targetIndex { return nums[index] } elseif index < targetIndex { left = index +1 } else { right = index -1 } } }
funcpatitation(_nums: inout [Int], _left: Int, _right: Int) -> Int { guard left < right else { return left } var begin = left var end = right let povit = nums[begin] while begin < end { while begin < end && nums[end] >= povit { end -=1 } if begin < end { nums[begin] = nums[end] begin +=1 } while begin < end && nums[begin] < povit { begin +=1 } if begin < end { nums[end] = nums[begin] end -=1 } } nums[begin] = povit return begin }