Kth Largest Element - Implementation

Here is the implementation using QuickSelect:

function findKthLargest(nums, k):
    target = nums.length - k
    return quickSelect(nums, 0, nums.length - 1, target)

function quickSelect(nums, left, right, target):
    pivotIdx = partition(nums, left, right)

    if pivotIdx == target:
        return nums[pivotIdx]
    else if pivotIdx < target:
        return quickSelect(nums, pivotIdx + 1, right, target)
    else:
        return quickSelect(nums, left, pivotIdx - 1, target)

Average O(n)O(n) time, worst case O(n2)O(n^2). Use random pivot to avoid worst case. Space is O(1)O(1) (excluding input) with tail recursion.